Reputation: 1387
Is that ever possible to initialize an Object instance, perform an operation on it and return this instance without creating a temporary variable?
For example:
def create_custom_object
Object.new do |instance|
instance.define_singleton_method(:foo) { 'foo' }
end
end
# returns an instance, but defines nothing :(
def create_custom_object
Object.new do
self.define_singleton_method(:foo) { 'foo' }
end
end
# same thing
rather than:
def create_custom_object
object = Object.new
object.define_singleton_method(:foo) { 'foo' }
object
end
Upvotes: 3
Views: 174