John Smith John Smith - 7 months ago 29
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 = Object.new {def people() return @people_arr end}


Whats the shortest code you know to do that? Thanks

Answer

You can define a singleton method on that object like:

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