Created
February 26, 2009 14:47
-
-
Save fdutey/70883 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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