我正在尝试动态定义调用另一个带有options参数的函数的函数:
class MyClass
["hour", "minute", "second"].each do |interval|
define_method "get_#{interval}" do |args|
some_helper(interval, args)
end
end
def some_helper(interval, options={})
# Do something, with arguments
end
end
Run Code Online (Sandbox Code Playgroud)
我希望能够以这两种方式在MyClass上调用不同的方法(使用和不使用可选参数):
mc = MyClass.new
mc.get_minute( :first_option => "foo", :second_option => "bar")
mc.get_minute # This fails with: warning: multiple values for a block parameter (0 for 1)
Run Code Online (Sandbox Code Playgroud)
在第二次拨打分钟时,我看到了这个警告:
警告:块参数的多个值(0表示1)
我知道|| =运算符,但不要认为它会帮助我...尝试创建一个数组来计算对象数组中"类型"的数量.
array.each do |c|
newarray[c.type] = newarray[c.type] ? newarray[c.type]+1 ? 0
end
Run Code Online (Sandbox Code Playgroud)
有没有更优雅的方式来做到这一点?
我对Ruby的<=>运算符很困惑.它与==或===有什么不同?任何综合的例子/用例?谢谢.
假设我有一个proc,proc包含几个语句和函数调用.我怎么知道这个函数到目前为止花了多少时间?
Ruby(和Perl)有一个触发器的概念:
file = File.open("ordinal")
while file.gets
print if ($_ =~ /third/) .. ($_ =~ /fifth/)
end
Run Code Online (Sandbox Code Playgroud)
给出了一系列序数,例如
first
second
third
fourth
fifth
sixth
Run Code Online (Sandbox Code Playgroud)
当它达到"第三"时开始打印,当达到"第五"时停止打印:
third
fourth
fifth
Run Code Online (Sandbox Code Playgroud)
是否存在类似于此的函数式编程概念,或者通常用takewhiles 来描述?我不是在问一个特定的语言,而是用你用来形容它的术语.
如果我执行这个ruby代码:
def foo
100
end
p defined?(foo), foo
if false
foo = 200
end
p defined?(foo), foo
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
"method"
100
"local-variable"
nil
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么foo设置为nil不执行if后?这是预期的行为还是红宝石?
我希望从0.0001到1计数,在ruby中有0.0001步.我写了这段代码,但它进入了无限循环.为什么口译员做错了总结.
x = 0.0001
while x != 1.0
puts x
x = x + 0.0001
end
Run Code Online (Sandbox Code Playgroud)
这是它给出的前10个值:
0.0001
0.0002
0.00030000000000000003
0.0004
0.0005
0.0006000000000000001
0.0007000000000000001
0.0008000000000000001
0.0009000000000000002
0.0010000000000000002
Run Code Online (Sandbox Code Playgroud)
它应该是0.0001,0.0002,0.0003等......我怎样才能使它工作?谢谢!
假设我有一个看起来像这样的数组:
a = [cat, dog, cat, mouse, rat, dog, cat]
Run Code Online (Sandbox Code Playgroud)
我如何循环,并做重复的事情 - 例如说删除它们?
换句话说,如果我这样做a.each do |i|,我如何评价[0],反对[1],[2],[3] ...然后当我找到我想要的那个时,说一个[2]在这种情况下有第一个副本,然后我把它推到堆栈或删除它或东西.
我知道如何评估键,而不是值......但是如何在同一个数组中相互评估值?
谢谢.
是否存在从散列中删除键值对的非破坏性方法?
例如,如果你这样做了
original_hash = {:foo => :bar}
new_hash = original_hash
new_hash = new_hash.reject{|key, _| key == :foo}
Run Code Online (Sandbox Code Playgroud)
要么
original_hash = {:foo => :bar}
new_hash = original_hash
new_hash = new_hash.dup
new_hash.delete(:foo)
Run Code Online (Sandbox Code Playgroud)
然后original_hash没有改变,并且new_hash改变了,但它们有点冗长.但是,如果你这样做了
original_hash = {:foo => :bar}
new_hash = original_hash
new_hash.delete(:foo)
Run Code Online (Sandbox Code Playgroud)
然后original_hash改变了,这不是我想要的.
有没有一种方法可以满足我的需求?
Array#[] =的文档说明了这一点
如果索引大于数组的当前容量,则数组会自动增长.
当它自动增长时,它会使用以下nil值:
arr = []
arr[2] = "!"
arr # => [nil, nil, "!"]
Run Code Online (Sandbox Code Playgroud)
是否可以指定前两个值的默认值?
目前,我正在做
arr = []
index = 2
currently_uninitialized_value_range = (arr.length)...(index)
default_values = currently_uninitialized_value_range.map{ "" }
arr[currently_uninitialized_value_range] = default_values
arr[index] = "!"
arr # => ["", "", "!"]
Run Code Online (Sandbox Code Playgroud)
这有点冗长.
我使用的是数组,而不是哈希值,因为它们表示我将要输入到电子表格中的值,而我正在使用的库(Axlsx)更喜欢逐行添加数据.
ruby ×9
arrays ×3
flip-flop ×1
hash ×1
perl ×1
proc-object ×1
side-effects ×1
tcl ×1
time ×1
timestamp ×1