John Smith John Smith - 1 year ago 87
Ruby Question

Create object with methods

I would like to create an object with one simple get method:
I would like to use a valid code that looks somehow like that:

obj = {def people() return @people_arr end}

Whats the shortest code you know to do that? Thanks

Answer Source

You can define a singleton method on that object like:

obj =
obj.define_singleton_method(:your_method) { puts 'this is my awesome method only defined for this object' }