考虑这个人为的例子:
# Dispatch on value of fruit_kind:
TYPE_A = :apple
TYPE_B = :banana
TYPE_C = :cherry
eating_method = nil
case fruit_kind
# Methods to use for different kinds of fruit (assume these are
# already defined)
when TYPE_A then eating_method = bite
when TYPE_B then eating_method = peel
when TYPE_C then eating_method = om_nom_nom
end
Run Code Online (Sandbox Code Playgroud)
现在我想eating_method用一些参数来调用目标:
# Doesn't work; this tries to invoke a method called "eating_method",
# not the reference I defined earlier.
eating_method(some_fruit)
Run Code Online (Sandbox Code Playgroud)
在Ruby中这样做的正确方法是什么?
例如,在Python中,我可以创建一个这样的类:
class foo(object):
bar = 'x'
def __init__(self, some_value):
self.some_attr = some_value
Run Code Online (Sandbox Code Playgroud)
...其中bar是类属性,some_attr是实例属性.在Ruby中做这样的事情的惯用方法是什么?
我有两个数据框,它们都有一个包含如下因素的列:
> head(test.data)
var0 var1 date store
1 109.5678 109.5678 1990-03-30 Store1
2 109.3009 108.4261 1990-06-30 Store1
3 108.8262 106.2517 1990-09-30 Store1
4 108.2443 108.6417 1990-12-30 Store1
5 109.5678 109.5678 1991-03-30 Store1
6 109.3009 108.4261 1991-06-30 Store1
> summary(test.data)
var0 var1 date store
Min. : -9.72 Min. : -2.297 Min. :1990-03-30 Store1 : 8
1st Qu.: 68.32 1st Qu.: 71.305 1st Qu.:1990-09-07 Store2 : 8
Median :102.19 Median :101.192 Median :1991-02-13 Store3 : 8
Mean :101.09 Mean :103.042 Mean :1991-02-13 …Run Code Online (Sandbox Code Playgroud) 编辑补充:Drat it!我没有最新版本的代码.这是一个错误,他修复了它.请和我一起投票结束.
我正在看别人的Python代码,上面写着:
bar = [].append(foo)
Run Code Online (Sandbox Code Playgroud)
我认为这是一种过于复杂的说法:
bar = [foo]
Run Code Online (Sandbox Code Playgroud)
我想知道他是否只是对Python语法一无所知,但通常他的Python代码似乎非常称职,而且在代码的另一个地方,他写道:
qux(param=[foo])
Run Code Online (Sandbox Code Playgroud)
所以,这并不能解释它.
我错过了什么吗?这是一个我不知道的有用的成语吗?(例如,它更高性能,适用于旧版本的Python等)
在Perl中,如果我想在对象构造函数中使用命名参数,如果我希望进行一些验证,我的代码似乎有点笨拙.
sub new {
my $class = shift;
my $self = {};
my %args = @_;
foreach my $argname (keys %args) {
if ($argname eq 'FOO') { $self->{$argname} = $args{$argname}; }
elsif ($argname eq 'BAR') { $self->{$argname} = $args{$argname}; }
elsif ($argname eq 'BAZ') { $self->{$argname} = $args{$argname}; }
…
else { die "illegal argument $argname\n"; }
}
bless $self;
return $self;
}
Run Code Online (Sandbox Code Playgroud)
首先,有一个临时的hash(%args)似乎有点笨拙.其次,整个if链条似乎冗长乏味.
后者可以简化为
if ('-FOO-BAR-BAZ-'=~m/-$argname-/) { $self->{$argname} = $args{$argname} }
else { die "..."; …Run Code Online (Sandbox Code Playgroud) 目前,我正在尝试用Python填充字典,但我认为我所做的有点多余.是否有更多pythonic方式来执行以下操作:
if not pattern_file_map.get(which_match):
pattern_file_map[which_match] = [line]
else:
pattern_file_map[which_match].append(line)
Run Code Online (Sandbox Code Playgroud)
哪里pattern_file_map是字典.
我知道在检查字典中是否有密钥时会有一定的习惯用法,比如 这个问题,但是我只想用一个列表来填充这个字典.
在rails中,update_attributes在模型上使用将创建基于的嵌套模型association_attributes.是否有一种惯用的方法来使其更新嵌套模型?
例如:
Message.rb:
attr_accessible :recipient_attributes
has_one :recipient
accepts_nested_attributes_for :recipient
Run Code Online (Sandbox Code Playgroud)
Recipient.rb
belongs_to :message
# has an name fied
# has an email field
Run Code Online (Sandbox Code Playgroud)
接受者
r = Recipient.create
r.create_recipient name: "John Smith", email: "john@gmail.com"
r.update_attributes recipient_attributes: {email: "johns_new_address@gmail.com"}
r.recipient.name # nil <-- this creates a NEW recipient, so the name is nil
r.recipient.email # johns_new_address@gmail.com
Run Code Online (Sandbox Code Playgroud)
相反,我希望r.recipient收到相同的收件人记录,但会收到一封新电子邮件.
编写以下内容的惯用方法是什么?
val starting_value = ...
val result1 = f1(startingValue)
val result2 = f2(result1)
...
val resultN = fN(resultN-1)
Run Code Online (Sandbox Code Playgroud)
如果starting_value是我想要应用这些功能的项目列表,我可以写
starting_list.map(f1).map(f2)...map(fN)
Run Code Online (Sandbox Code Playgroud)
我可以通过做类似的事来伪造这个
Some(starting_value).map(f1)....map(fN).get
Run Code Online (Sandbox Code Playgroud)
要么
List(starting_value).map(f1)....map(fN).head
Run Code Online (Sandbox Code Playgroud)
但这似乎不必要地混淆了.
注意:这个问题似乎有关,但似乎是关于下游问题.
鉴于以下内容:
type AStruct struct {
m_Map map[int]bool
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,在初始化AStruct之前不能使用实例AStruct.m_Map:
m_Map=make(map[int]bool,100)
Run Code Online (Sandbox Code Playgroud)
Init()在这种情况下,我已经为我的结构编写了一个函数:
func (s *AStruct) Init() {
s.m_Map=make(map[int]bool,100)
}
Run Code Online (Sandbox Code Playgroud)
我并不特别关心这个设计,因为它需要(s *AStruct) Init()是公共的,并且要求客户端在使用实例之前明确地调用它AStuct- 在临时中有一个不可用的实例AStuct,等待生成一个panic.
在初始化完所有内容后,我可以设置init()私有并initialized bool在structset中声明一个标志并检查每个方法:trueinit()
func (s *AStruct) DoStuff {
if !s.initialized {
s.init()
}
s.m_Map[1]=false
s.m_Map[2]=true
}
Run Code Online (Sandbox Code Playgroud)
但这很尴尬,并添加了多余的代码.
在Go中有没有标准的处理方式?保证m_Map的一个将被初始化而不依赖于客户端来调用Init()?
这似乎是一个之前会被问过的问题,但我找不到它.(如果你这样做,请指出我并将其作为副本关闭.)
我有C代码,其工作方式如下:
printf("some characters");
for (; n >= 0; n--)
{
printf(", %s", f(n, k));
}
printf("more characters");
Run Code Online (Sandbox Code Playgroud)
对于一些在这里不重要的变量n和k函数char* f(int n, int k).(n并且k不是常数.)我想将上面的代码转换为一个函数,它返回一个char*而不是简单地使用printf它来显示它.(它最终将被打印,但这可以让我做一些重构,当然更容易测试.)当然,我可以创建字符串并逐个字符地复制它们但肯定有更好的(更干净,更快,更惯用的方式这样做.
在我的特定应用程序中f是一个非常简单的函数,itos它是一种相对的函数,但是由于它不能以任何有意义的方式缓存k.但我欢迎那些对这种情况有益的想法,因为它可能对将来的其他人有用.
idioms ×10
python ×2
ruby ×2
activerecord ×1
c ×1
constructor ×1
dictionary ×1
go ×1
join ×1
loops ×1
oop ×1
parameters ×1
perl ×1
r ×1
scala ×1
string ×1