特定
a = [[:a, :b, :c]]
Run Code Online (Sandbox Code Playgroud)
1)我理解这一点
a.each{|(x, y), z| p z} # => :b
Run Code Online (Sandbox Code Playgroud)
有两个变量(x, y)和z,所以第三个元素:c是扔掉,和z比赛:b.我理解这一点
a.each{|(x, y), z| p y} # => nil
Run Code Online (Sandbox Code Playgroud)
该(x, y)匹配:a,并且由于它是不是一个数组,有到它没有元素,因此y相匹配nil.
但是怎么做
a.each{|(x, y), z| p x} # => :a
Run Code Online (Sandbox Code Playgroud)
工作?我希望nil能够回来.
2)为什么返回值是这样的?
a.each{|(x, y)| p x} #=> :a
a.each{|(x, y)| p y} #=> :b
Run Code Online (Sandbox Code Playgroud)
我希望他们都能回归nil.
如果我运行以下代码,前两行将返回我所期望的.但是,第三个返回2的二进制表示.
2.to_s # => "2"
2.to_s * 2 # => "22"
2.to_s *2 # => "10"
Run Code Online (Sandbox Code Playgroud)
我知道在调用时传入2 to_s会将我的输出转换为二进制,但为什么to_s忽略*第三种情况呢?如果这有任何区别,我正在运行Ruby 1.9.2.
您可以使用 splat 运算符来解构数组。
def foo(arg1, arg2, arg3)
#...Do Stuff...
end
array = ['arg2', 'arg3']
foo('arg1', *array)
Run Code Online (Sandbox Code Playgroud)
但是有没有办法破坏选项类型的哈希值呢?
def foo(arg1, opts)
#...Do Stuff with an opts hash...
end
opts = {hash2: 'bar', hash3: 'baz'}
foo('arg1', hash1: 'foo', *opts)
Run Code Online (Sandbox Code Playgroud)
如果不是原生 ruby,Rails 是否添加了类似的东西?
目前我正在做这件事
foo('arg1', opts.merge(hash1: 'foo'))
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
function test(...$strings)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
它允许我像这样调用 test() :
test('a', 'series of', 'strings go here');
Run Code Online (Sandbox Code Playgroud)
这有效。然而,我经常想做:
test([ 'a', 'series of', 'strings go here' ]);
Run Code Online (Sandbox Code Playgroud)
或者:
test($an_array_of_strings);
Run Code Online (Sandbox Code Playgroud)
事实上,我非常确定这有效,以至于当我开始收到有关“数组到字符串转换”的错误时,我感到震惊和困惑。
我的意思是,“...”语法是一种特殊的语言构造,专门用于将函数的可变数量的参数转换为数组!我什至不明白为什么它不会自动理解这种非常常见/有用的做法。
既然它(显然)没有,是否有某种方法可以完成此任务而不必使用两个单独的函数?我不想要一个单独的:
function test_array($strings = [])
{
// ...
}
Run Code Online (Sandbox Code Playgroud) 如何在不使用ECMA6 功能的情况下跨对象进行喷射?
function can(arg0, arg1) {
return arg0 + arg1;
}
function foo(bar, haz) {
this.bar = bar;
this.haz = haz;
}
myArgs = [1,2];
Run Code Online (Sandbox Code Playgroud)
我可以这样can做:
can.apply(this, myArgs);
Run Code Online (Sandbox Code Playgroud)
尝试使用时foo:
new foo.apply(this, myArgs);
Run Code Online (Sandbox Code Playgroud)
我收到此错误(因为我正在调用new):
TypeError: function apply() { [native code] } is not a constructor
Run Code Online (Sandbox Code Playgroud) 我的理解是,非数组对象上的单个 splat 调用to_a然后将元素分离。由于nil.to_a被定义为[],因此会发生以下转换:
[:foo, *nil, :bar]
# => [:foo, *nil.to_a, :bar]
# => [:foo, *[], :bar]
# => [:foo, :bar]
Run Code Online (Sandbox Code Playgroud)
通过类比,我认为非哈希对象上的 double splat 调用to_h然后将键值对分离。由于nil.to_h被定义为{},我预计会发生以下转换:
{"foo" => 1, **nil, "bar" => 2}
# => {"foo" => 1, **nil.to_h, "bar" => 2}
# => {"foo" => 1, **{}, "bar" => 2}
# => {"foo" => 1, "bar" => 2}
Run Code Online (Sandbox Code Playgroud)
但实际上,它会引发一个错误:no implicit conversion of nil into Hash。为什么它会这样?
编辑我不是在问设计背后的原因。我想问我关于双打的想法哪里错了。
如何从以下代码中删除“警告:'*'解释为参数前缀”?
hash = {"a" => 1,
"b" => 2,
"s" => 3,}
if "string".start_with? *hash.keys then
puts "ok"
else
puts "ng"
end
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我得到:
$ ruby -w /tmp/a.rb
/tmp/a.rb:5: warning: `*' interpreted as argument prefix
ok
Run Code Online (Sandbox Code Playgroud)
解决此警告的最佳方法是什么?
我试图hash像这样把括号括起来:
hash = {"a" => 1,
"b" => 2,
"s" => 3,}
if "string".start_with? (*hash.keys) then
puts "ok"
else
puts "ng"
end
Run Code Online (Sandbox Code Playgroud)
然后您得到:
$ ruby -w /tmp/a.rb
/tmp/a.rb:5: syntax error, unexpected *
if "string".start_with? (*hash.keys) then
^
/tmp/a.rb:5: syntax error, unexpected ')', …Run Code Online (Sandbox Code Playgroud) 案例陈述:
case x
when 1
"one"
when 2
"two"
when 3
"three"
else
"many"
end
Run Code Online (Sandbox Code Playgroud)
使用运算符进行评估===。该运算符在表达式的值上调用,when并将表达式的值case作为参数。上面的 case 语句等价于以下内容:
if 1 === x
"one"
elsif 2 === x
"two"
elsif 3 === x
"three"
else
"many"
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下:
A = 1
B = [2, 3, 4]
case reason
when A
puts "busy"
when *B
puts "offline"
end
Run Code Online (Sandbox Code Playgroud)
该when *B部分无法重写为*B === 2.
这是关于 splat 运算符吗?splat 运算符是关于赋值,而不是比较。case语句如何处理when *B?
splat ×10
ruby ×8
hash ×2
arguments ×1
arrays ×1
double-splat ×1
ecmascript-5 ×1
function ×1
javascript ×1
literals ×1
parentheses ×1
php ×1
ruby-1.9.2 ×1
ruby-1.9.3 ×1
syntax ×1