我今天在浏览Rails代码时偶然发现了这个片段:
new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
Run Code Online (Sandbox Code Playgroud)
什么是星号 - 双结肠(如果你愿意的话,还是splat-le-colon)*::Date呢?
据推测,它与特定命名空间的Date类的范围有关......但是,作者必须包含它而不仅仅是使用标准Date类.
在玩了对象id之后我发现了这个.
ObjectSpace._id2ref(2648)
=> :**
ObjectSpace._id2ref(6688)
=> :**
ObjectSpace._id2ref(2648) == ObjectSpace._id2ref(6688)
=> false
Run Code Online (Sandbox Code Playgroud)
第一个是求幂运算符的符号;
2.send(ObjectSpace._id2ref(2648), 3)
=> 8
2.send(ObjectSpace._id2ref(6688), 3)
NoMethodError: undefined method `**' for 2:Fixnum
Run Code Online (Sandbox Code Playgroud)
但第二个不知何故不是?我认为它们在传递给#print之后看起来一样.但是,什么是区别?其中一个是某种形式的unicode符号吗?
更新:第二个可能是关键字参数的新双splat,但我似乎无法验证这一点.
我正在尝试编写一些PowerShell函数来执行某些操作,然后透明地调用现有的内置函数.我想传递所有未触及的论点.我不想知道论点的任何细节.
我厌倦了使用'splat'来做这件事@args但是没有按照我的预期工作.
在下面的例子中,我写了一个叫做myls打印你好的玩具函数!然后调用相同的内置函数,Get-ChildItem内置别名ls调用其余参数行完整.到目前为止我的工作得很好:
function myls
{
Write-Output "hello!"
# $MyInvocation | Format-List # <-- uncomment this line for debug info
Invoke-Expression ("Get-ChildItem " + $MyInvocation.UnboundArguments -join " ")
}
Run Code Online (Sandbox Code Playgroud)
正确的版本myls应该能够处理没有参数的调用,带有一个带有命名参数的参数,来自包含多个分号分隔命令的行,并且参数中的变量包括包含空格的字符串变量.基本上,它应该是替代品ls.
下面的测试比较myls和内置ls:
[注意:输出省略和/或压缩以节省空间]
PS> md C:\p\d\x, C:\p\d\y, C:\p\d\"jay z"
PS> cd C:\p\d
PS> ls # no args
PS> myls # pass
PS> cd ..
PS> ls d # one arg
PS> …Run Code Online (Sandbox Code Playgroud) 在查看raphael或g.raphael或其他库的源代码时,我注意到开发人员做了类似这样的事情:
var val = Math.max.apply(Math, data_array);
Run Code Online (Sandbox Code Playgroud)
为什么不直接调用函数,例如:
var val = Math.max(data_array);
Run Code Online (Sandbox Code Playgroud)
谢谢.
你如何重写拆包语法的结果*obj和**obj?
例如,你能以某种方式创建一个thing行为如下的对象:
>>> [*thing]
['a', 'b', 'c']
>>> [x for x in thing]
['d', 'e', 'f']
>>> {**thing}
{'hello world': 'I am a potato!!'}
Run Code Online (Sandbox Code Playgroud)
注意:迭代通过__iter__("for x in thing")返回*splat unpack中的不同元素.
我一看operator.mul和operator.pow,但这些功能只关心用途有两个操作数,如a*b和a**b,而且似乎无关的图示操作.
如果我有一个数组(在运行时之前长度未知),有没有办法调用一个函数,并将数组的每个元素作为一个单独的参数?
像这样:
foo = @(varargin) sum(cell2mat(varargin));
bar = [3,4,5];
foo(*bar) == foo(3,4,5)
Run Code Online (Sandbox Code Playgroud)
上下文:我有一个n-d数组的索引列表Q.我想要的是什么Q(a,b,:),但我只有[a,b].由于我不知道n,我不能只是硬编码索引.
matlab operators multidimensional-array splat matrix-indexing
为splat参数设置默认值会产生错误:
1.9.3-p374 :001 > def a b, *c = nil
1.9.3-p374 :002?> end
SyntaxError: (irb):1: syntax error, unexpected '=', expecting ';' or '\n'
def a b, *c = nil
^
from /Users/me/.rvm/rubies/ruby-1.9.3-p374/bin/irb:16:in `<main>'
Run Code Online (Sandbox Code Playgroud)
我试过的一些变化也不起作用:
1.9.3-p374 :003 > def a b, *c = []
1.9.3-p374 :005 > def a b, (*c) = nil
1.9.3-p374 :007 > def a b, (*c = [])
1.9.3-p374 :009 > def a b, (*c = [1,2,3])
1.9.3-p374 :011 > def a b, *c = [1,2,3]
Run Code Online (Sandbox Code Playgroud)
我在这里看不到不确定性问题,所以看起来应该是可能的. …
说我有一个参数列表:
> (setf format-args `(t "it's ~a" 1))
(T "it's ~a" 1)
Run Code Online (Sandbox Code Playgroud)
然后,我如何将其"展开"或"展开"为一系列参数而不是单个列表参数,以提供给格式函数?即我想要进行以下函数调用:
> (format t "it's ~a" 1)
Run Code Online (Sandbox Code Playgroud)
作为参考,我将在python或ruby中编写以下内容:
format(*format-args)
Run Code Online (Sandbox Code Playgroud)
我确信它可以做到,但也许我认为这是错误的.这项行动的名称似乎没有得到很好的同意,这也无济于事......
哈希上的splat将其转换为数组.
[*{foo: :bar}] # => [[:foo, :bar]]
是否有一些隐藏的机制(如隐式类转换)在这里,或者它是一个内置的原始功能?
除了一个数组,nil在Ruby 1.9下使用splat运算符消除/改变的唯一东西是哈希吗?
我注意到我发现**Ruby 2.1.1中的(双splat)运算符是一个非常令人惊讶的行为.
当在a之前使用键值对时**hash,哈希保持不变; 但是,当键值对仅在之后使用时**hash,哈希值将被永久修改.
h = { b: 2 }
{ a: 1, **h } # => { a: 1, b: 2 }
h # => { b: 2 }
{ a: 1, **h, c: 3 } # => { a: 1, b: 2, c: 3 }
h # => { b: 2 }
{ **h, c: 3 } # => { b: 2, c: 3 }
h # => { b: 2, c: …Run Code Online (Sandbox Code Playgroud) splat ×10
ruby ×5
double-splat ×2
hash ×2
syntax ×2
common-lisp ×1
javascript ×1
matlab ×1
null ×1
objectid ×1
operators ×1
powershell ×1
python ×1
ruby-1.9 ×1
scope ×1
symbols ×1