Skip to content

Instantly share code, notes, and snippets.

@fdutey fdutey/gist:70883
Created Feb 26, 2009

Embed
What would you like to do?
class Object
def with(obj, &block)
obj.instance_eval &block
end
end
h = { :a => 1, :b => 2 }
a = ["a", "b"]
with h do
puts keys
puts values
#...
puts a.size
end
#=> b\na\n2\n1\n2
@person = Person.new
with @person do
self.firstname = "Florian"
self.lastname = "Dutey"
self.email = "fdutey@example.com"
self.url = "http://blog.fdutey.com"
end
#self est obligatoire car sinon ruby considère que c'est une variable locale, comme au sein de l'objet.
@person.lastname #=> "Dutey"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.