[英]What is the Ruby equivalent of PHP's compact?
我的原始答案得到了显着改善。 如果从Binding
本身继承,它会更清晰。 to_sym
就在那里,因为旧版本的ruby将local_variables
作为字符串。
实例方法
class Binding
def compact( *args )
compacted = {}
locals = eval( "local_variables" ).map( &:to_sym )
args.each do |arg|
if locals.include? arg.to_sym
compacted[arg.to_sym] = eval( arg.to_s )
end
end
return compacted
end
end
用法
foo = "bar"
bar = "foo"
binding.compact( "foo" ) # => {:foo=>"bar"}
binding.compact( :bar ) # => {:bar=>"foo"}
原始答案
这是我能得到的最接近Php compact
方法的方法 -
方法
def compact( *args, &prok )
compacted = {}
args.each do |arg|
if prok.binding.send( :eval, "local_variables" ).include? arg
compacted[arg.to_sym] = prok.binding.send( :eval, arg )
end
end
return compacted
end
示例用法
foo = "bar"
compact( "foo" ){}
# or
compact( "foo", &proc{} )
但它并不完美,因为你必须通过一个过程。我愿意接受如何改进它的建议。
这是Bungus回答的一个变种,但是这里的单行是非常丑陋的,但不会扩展Binding或任何东西:
foo = :bar
baz = :bin
hash = [:foo, :baz].inject({}) {|h, v| h[v] = eval(v.to_s); h }
# hash => {:baz=>:bin, :foo=>:bar}
您也可以通过滥用块绑定使其看起来像一个方法调用 - 再次,Bungus的原始答案的变体:
module Kernel
def compact(&block)
args = block.call.map &:to_sym
lvars = block.binding.send(:eval, "local_variables").map &:to_sym
(args & lvars).inject({}) do |h, v|
h[v] = block.binding.send(:eval, v.to_s); h
end
end
end
foo = :bar
baz = :bin
compact {[ :foo, :bar, :baz ]}
# {:foo=>:bar, :baz=>:bin}
(我只是告诉自己{[..]}
是垃圾压缩符号。)
如果你使用binding_of_caller
gem,你可以放弃proc 和显式绑定:
require 'binding_of_caller'
module Kernel
def compact(*args)
lvars = binding.of_caller(1).send(:eval, "local_variables").map &:to_sym
(args.map(&:to_sym) & lvars).inject({}) do |h, v|
h[v] = binding.of_caller(2).send(:eval, v.to_s); h
end
end
end
foo = :bar
baz = :bin
compact :foo, :bar, :baz
# {:foo=>:bar, :baz=>:bin}
请注意,它很慢。 在生产代码中,你可能永远不应该尝试这样做,而只是保留一个值的哈希,以便程序员必须在你没有追捕并在睡眠中杀死你之后保持这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.