Is there a Ruby equivalent to PHP's extract?

后端 未结 2 1012
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 06:35

I can create a block that will extract hash elements and turn them into local variables, but I\'m wondering if a native method already exists. Something like this:



        
2条回答
  •  失恋的感觉
    2021-01-18 07:05

    You can use the each method to iterate through each key=>value pair:

    { :foo => 'bar', :foo2 => 'bar2' }.each do |key, value|
      print key,"\t",value,"\n"
    end
    

    Outputs:

     foo     bar
     foo2    bar2
    

提交回复
热议问题