如何拆分哈希并将其作为红宝石中的两个独立参数传递?

时间:2018-07-06 21:59:52

标签: ruby

url = 'SomeUrl'
args{ someKey: 'Value',
      key2: 'Value2'

}

该方法当前将这两个都用作参数。我将这两个参数合并为一个散列,如下所示:

opts = {
                 Hello: {
                   Name: 'John',
                   Version: '1.0'

                 },
                 World: {
                   platformVersion: '3.4x',
                   helloVersion: '2.3'
                 }
               }

我正在尝试的是将这两个哈希结合起来,并在运行时通过这种方法将它们分开:

   def initialize(opts)
         #this is how it was done before
          SomeClass.for(:remote, opts)
         #What I want here is to split that hash into two arguments
    end

1 个答案:

答案 0 :(得分:0)

opts = {
  Hello: {
    Name: 'John',
    Version: '1.0'
  },
  World: {
    platformVersion: '3.4x',
    helloVersion: '2.3'
  }
}

def initialize(opts)
      SomeClass.for(:remote, opts[:Hello], opts[:World])
end

这是您想要的吗? SomeClass.for方法将收到三个参数:

  1. :remote
  2. { Name: 'John', Version: '1.0' }
  3. { platformVersion: '3.4x', helloVersion: '2.3' }