开发者

Method expects object to have 2 properties, but the object I have has it in an array?

开发者 https://www.devze.com 2023-03-09 16:56 出处:网络
There is a method that looks like: def some_thing(user) x = user.age y = 开发者_C百科user.height end

There is a method that looks like:

def some_thing(user)
  x = user.age
  y = 开发者_C百科user.height
end

I have an object that looks like:

  x = other.info[0]
  y = other.info[1]

How could I pass this information to the method some_thing?

some_thing(other)

won't work as it doesn't have the properties exposed in that manner.

Is it possible to do without modifying the classes and method parameters?


Create an anonymous Struct to create the properties and use Array splat to fill them:

some_thing(Struct.new(:age, :height).new(*other.info))
0

精彩评论

暂无评论...
验证码 换一张
取 消