6

给定一些局部变量compact,在 Ruby中最简单的方法是什么?

def foo
  name = 'David'
  age = 25
  role = :director
  ...
  # How would you build this:
  # { :name => 'David', :age => 25, :role => :director }
  # or
  # { 'name' => 'David', 'age' => 25, 'role' => :director }
end

在 PHP 中,我可以简单地这样做:

$foo = compact('name', 'age', 'role');
4

2 回答 2

9

我对我的原始答案提出了重大改进。如果你从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{} )

但它并不完美,因为你必须传入一个 proc。我愿意接受有关如何改进它的建议。

于 2013-07-02T11:33:26.897 回答
2

这是 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_callergem,您可以同时放弃 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}

请注意,它很慢。在生产代码中,您可能永远不应该尝试这样做,而只是保留一个值的哈希值,这样在您之后必须维护它的程序员就不会追捕您并在您的睡眠中杀死您。

于 2013-07-03T09:58:47.083 回答