给定一个数组文字,我想创建一个哈希,其中键是数组中的元素,值是包含其他/剩余元素的数组.
输入:
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
输出:
{1=>[2, 3], 2=>[1, 3], 3=>[1, 2]}
Run Code Online (Sandbox Code Playgroud)
如果我引入变量很容易:
arr = [1, 2, 3]
arr.map { |i| [i, arr - [i]] }.to_h
Run Code Online (Sandbox Code Playgroud)
但是使用数组文字,我能想出的唯一解决方案涉及instance_exec或者instance_eval看起来像是hackish:
[1, 2, 3].instance_exec { map { |i| [i, self - [i]] } }.to_h
Run Code Online (Sandbox Code Playgroud)
我是否忽略了内置方法或明显的解决方案?group_by,combination,permutation和partition似乎并没有帮助.
我跑了这个..
rails g scaffold shared_drive drive_name:string drive_path:string security_group_read:string security_group_modify:string
Run Code Online (Sandbox Code Playgroud)
当我打开路线时localhost:3000/shared_drives/new,标题会显示New Shared Drife
当我尝试在此页面上创建新对象时,我收到此错误
ActionController::ParameterMissing in SharedDrivesController#create
param is missing or the value is empty: shared_drife
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事?!?!为什么Rails改变了我的模型名称?
While monkey-patching a module from a Rails engine, we found that if you prepend a module B to another module A, the prepended module B won't be added to the ancestors of classes that have already included module A prior to the prepend. To illustrate:
module A
def a
puts 'a'
end
end
class B
include A
end
module C
def a
puts 'c'
end
A.prepend self
end
class D
include A
end
B.new.a # prints: a
D.new.a # …Run Code Online (Sandbox Code Playgroud) 根据模块和类的文档,调用super(不带参数或括号)使用相同的参数调用父方法:
在没有任何参数的情况下
super使用时,使用给予子类方法的参数.
为"参数变量"分配新值似乎改变了这种行为:
class MyClass
def foo(arg)
puts "MyClass#foo(#{arg.inspect})"
end
end
class MySubclass < MyClass
def foo(arg)
puts "MySubclass#foo(#{arg.inspect})"
super
arg = 'new value'
super
end
end
MySubclass.new.foo('inital value')
Run Code Online (Sandbox Code Playgroud)
输出:
MySubclass#foo("inital value")
MyClass#foo("inital value")
MyClass#foo("new value") # <- not the argument given to MySubclass#foo
Run Code Online (Sandbox Code Playgroud)
这是预期的吗?
这似乎是位置和关键字参数的预期行为,但它不适用于块参数:
class MyClass
def foo(&block)
puts "MyClass#foo { #{block.call.inspect} }"
end
end
class MySubclass < MyClass
def foo(&block)
puts "MySubclass#foo { #{block.call.inspect} }"
super …Run Code Online (Sandbox Code Playgroud) 在jquery中是否有任何方法,如$ .post,$ .ajax等,其工作方式与普通表单提交相同.我知道.submit()但它需要一个表单元素,我们可以在jquery中发送没有表单元素的正常请求吗?
a = [1, 2, 3]
b = [4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
如何在2D数组中组合两个数组?:
[[1, 4], [2, 5], [3, 6]]
Run Code Online (Sandbox Code Playgroud) 我只想在Ruby中连接多个数组,并且找不到令人满意的方法.
输入示例:
foo = [1, 2, 3]
bar = [4, 5, 6]
baz = [7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
预期结果:(不修改现有数组)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
我的实际数组要大得多,所以我对一个有效的解决方案很感兴趣.可能还有三个以上的数组,因此首选短语法.
foo + bar + baz是显而易见的,它简洁明了.但它被评估为(foo + bar) + baz.换句话说:它创建一个[1, 2, 3, 4, 5, 6]在整个操作后被抛弃的中间数组.如文档中所述:
重复使用
+=数组可能效率很低
[*foo, *bar, *baz]基本上内联对大型数组效率不高的元素.它看起来更像是对我的黑客攻击.
[foo, bar, baz].flatten(1) 看起来比上面更糟糕,表现明智.
[].concat(foo).concat(bar).concat(baz) 是最快的,但它看起来很麻烦,需要多个方法调用.
对于这样的基本任务,不应该有简单的类方法吗?就像是:
Array.concat(foo, bar, baz)
Run Code Online (Sandbox Code Playgroud)
我错过了一些明显的东西吗
由于 proc 是一个对象,我可以在它自己的实例范围内创建一个 proc 吗?
例如:
prc = Proc.new do
foo
end
def prc.foo
123
end
prc.call
# NameError: undefined local variable or method `foo' for main:Object
Run Code Online (Sandbox Code Playgroud)
通过更改self或明确receiver引用 proc。
该接收器必须动态评估,例如以下应该工作:
other_prc = prc.clone
def other_prc.foo
456
end
other_prc.call
#=> 456 <- not 123
Run Code Online (Sandbox Code Playgroud)
这意味着我不能通过以下方式对其进行“硬编码”:
prc = Proc.new do
prc.foo
end
Run Code Online (Sandbox Code Playgroud)
换句话说:有没有办法从 proc 内部引用 procs 实例?
另一个没有的例子foo:(放什么# ???)
prc = Proc.new do
# ???
end
prc == prc.call #=> true
other_prc = prc.clone …Run Code Online (Sandbox Code Playgroud) 我也很喜欢使用带红宝石的导轨.
我只想获得I18n.l一天的名字或一个月的名字,没有完整的日期或时间.因为我没有它.
例如,我只是想以国际化为例
= l Date::DAYNAMES[0]
Run Code Online (Sandbox Code Playgroud)
要么
= l Date::MONTHNAME[0]
Run Code Online (Sandbox Code Playgroud)
可能吗?
我有一个数组:
arr=[[1,2,3],[4,5],[6]],
Run Code Online (Sandbox Code Playgroud)
我有以下代码:
arr.transpose
Run Code Online (Sandbox Code Playgroud)
但它不起作用,如何解决?
我正进入(状态
[[1,2,3],[4,5],[6]].transpose
IndexError: element size differs (2 should be 3)
from (irb):13:in `transpose'
from (irb):13
from /home/durrant
Run Code Online (Sandbox Code Playgroud)
我的解决方案
arr.reduce(&:zip).map(&:flatten)
Run Code Online (Sandbox Code Playgroud)
输出:
[[1, 4, 6], [2, 5, nil], [3, nil, nil]]
Run Code Online (Sandbox Code Playgroud) ruby ×8
arrays ×3
closures ×1
javascript ×1
jquery ×1
prepend ×1
proc ×1
rails-i18n ×1
transpose ×1