Created
March 7, 2011 13:40
-
-
Save tpo/858515 to your computer and use it in GitHub Desktop.
Pascal's WITH in ruby
This file contains hidden or 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
# for a long time I wondered why Ruby didn't have Pascal's "WITH" | |
# construct. It's completely trivial to implent though... | |
def with object, &block | |
object.instance_eval(&block) | |
end | |
# now instead of needing to name your object all the time: | |
object = Object.new | |
object.foo | |
object.bazbar | |
# you can instead just do | |
with object do | |
foo | |
bazbar | |
end | |
# oh the simple wonders of metaprogramming :-o :-))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment