Skip to content

Instantly share code, notes, and snippets.

@fdutey
Created February 26, 2009 14:47
Show Gist options
  • Save fdutey/70883 to your computer and use it in GitHub Desktop.
Save fdutey/70883 to your computer and use it in GitHub Desktop.
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 = "[email protected]"
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