开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜