John Smith
John Smith

Reputation: 6259

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

Upvotes: 0

Views: 50

Answers (1)

Nobita
Nobita

Reputation: 23713

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' }

Upvotes: 2

Related Questions