我有三个变量,所有这些变量都可以是nil:
to
cc
bcc
Run Code Online (Sandbox Code Playgroud)
X如果其中任何一个都可以为零(但不是全部三个),我如何分配给/ cc/bcc中的任何一个?看起来很难看
if to.nil? and cc.nil?
X = bcc
elsif ...
Run Code Online (Sandbox Code Playgroud)
你做不到
X = to or cc or bcc
Run Code Online (Sandbox Code Playgroud)
无论是.
我知道这段代码可能不太正确:
def print_string(&str)
puts str
end
print_string{"Abder-Rahman"}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,这就是我得到的:
#<Proc:0x03e25d98@r.rb:5>
Run Code Online (Sandbox Code Playgroud)
这个输出是什么?
我不想从外部(通过attr_accessor)设置类的类变量,然后从其中一个对象内部访问它.我正在使用ruby 1.9.2.这是我的代码:
class Service
def initialize(id)
@my_id = id
end
class << self
attr_accessor :shared_id
end
def system_id
@my_id + @@shared_id
end
end
Run Code Online (Sandbox Code Playgroud)
如果我设置Service.shared_id = "A2",然后调用Service.new("A").system_id,这不会返回"AA2".它显示以下错误:
服务中未初始化的类变量@@ shared_id
如果我没有设置Service.service_id,则行为就像.有人可以解释为什么会这样吗?
说我有一个像这样的数组:
[
{"timestamp"=>"1", "count"=>4488.0},
{"timestamp"=>"2", "count"=>4622.0},
{"timestamp"=>"3", "count"=>4655.0},
{"timestamp"=>"4", "count"=>4533.0},
{"timestamp"=>"5", "count"=>4439.0},
{"timestamp"=>"6", "count"=>4468.0},
{"timestamp"=>"7", "count"=>4419.0},
{"timestamp"=>"8", "count"=>4430.0},
{"timestamp"=>"9", "count"=>4429.0},
{"timestamp"=>"10", "count"=>4502.0},
{"timestamp"=>"12", "count"=>4497.0},
{"timestamp"=>"13", "count"=>4468.0},
{"timestamp"=>"14", "count"=>4510.0},
{"timestamp"=>"15", "count"=>4547.0},
{"timestamp"=>"16", "count"=>4471.0},
{"timestamp"=>"17", "count"=>4501.0},
{"timestamp"=>"18", "count"=>4451.0},
{"timestamp"=>"19", "count"=>4453.0},
{"timestamp"=>"20", "count"=>4593.0},
{"timestamp"=>"21", "count"=>4540.0},
{"timestamp"=>"22", "count"=>4516.0},
{"timestamp"=>"23", "count"=>4494.0}
]
Run Code Online (Sandbox Code Playgroud)
我想循环它将每个x项目放在一个新的数组中,所以就像说我想写一个方法,接受一个类似的my_arr.skip(5)东西?我有点卡在这里,不知道该怎么办.任何帮助都将非常感激.
我有一个数组:
x = [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
我想得到:
output = [1,2,6,24,120]
Run Code Online (Sandbox Code Playgroud)
希望你能看到模式以及我想在这里完成的任务.x[1]变成了x[1]*x[0].x[2]=> x[2]*x[1]*x[0]等
在Ruby中执行此操作的最有效方法是什么?
我想制作几个具有相同参数的对象,所以我尝试将它们存储在返回它们的proc中.但是解释器将返回结果评估为一个参数,而不是几个参数.我的过程是:
proc element_param {} {
return "-filled 1\
-visible 1\
-linewidth 1\
-linecolor yellow\
-fillcolor yellow\
-relief roundraised\
-linewidth 2"
}
Run Code Online (Sandbox Code Playgroud)
我用它:
$this/zinc add rectangle 1 [list "100" "100" "200" "200"] [element_param]
Run Code Online (Sandbox Code Playgroud)
如何将它们转换为几个不同的参数?
什么是星号变量*arr?
*arr = "sayuj"
=> ["sayuj"]
*arr = *%w{i am happy}
=> ["i", "am", "happy"]
*arr = %w{i am happy}
=> [["i", "am", "happy"]]
Run Code Online (Sandbox Code Playgroud) 目前正在编写一个类,其中我正在考虑私有的方法遍布整个代码.private我想在类的顶部做一个内联声明,而不是添加一行并复制粘贴它下面的所有内容private :foo, :bar.
但是,每当我尝试将参数声明为私有内联的方法时,我都会收到一条错误消息.举例来说,如果我有一个方法foo(bar, baz),并尝试声明它与私人private :foo(bar, baz)我得到的两个括号错误信息,希望kEND和=替代.
如果我尝试声明它private :foo,我会被告知foo在我的代码中没有这样的方法.
如何在不发生这些错误的情况下做我想做的事情?
我正在分析一些代码,我正在寻找字符串文字,以检查我是否有任何重复.例如,如果我有
def test_foo
input_filename = "foo.txt"
# ...
end
def test_bar
input_filename = "bar.txt" # Fine
# ...
end
def test_baz
# Bad! Refactor it to a constant that's shared by test_foo and test_baz
input_filename = "foo.txt"
# ...
end
Run Code Online (Sandbox Code Playgroud)
我希望分析程序告诉["foo.txt", "bar.txt", "foo.txt"]我源代码中存在的内容.
我怎样才能做到这一点?
好吧,我在互联网上搜索了答案,并在我的红宝石程序员中搜索了几个小时,但我无法对此进行排序.我正在编写一个脚本,用于从数组中的元素中进行各种组合.
ar = ["a","b","c","d"]
Run Code Online (Sandbox Code Playgroud)
在这一点上,我能够做出这些组合:
["a"],["a","b"],["a","b","c"],["a","b","c","d"],["b"],["b","c"],["b","c","d"],["c"],["c","d"],["d"]
Run Code Online (Sandbox Code Playgroud)
这没关系,但我找不到搜索这些组合的方法,例如["a","c"] or ["a","c","d"] or ["a","d"],等等......
现在我的代码看起来像:
def combinaties(array)
combinaties = []
i=0
while i <= array.length-1
combinaties << array[i]
unless i == array.length-1
array[(i+1)..(array.length-1)].each{|volgend_element|
combinaties<<(combinaties.last.dup<<volgend_element)
}
end
i+=1
end
end
Run Code Online (Sandbox Code Playgroud)