Created
July 1, 2010 09:02
-
-
Save rklemme/459749 to your computer and use it in GitHub Desktop.
Toy example of simpler loop nesting
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
11:01:47 Temp$ ./nested_each.rb | |
[["foo\n", "bar", "dummy"], ["another\nline", "ends", "now\nor not?"]] | |
Now the test... | |
"foo\n" | |
"bar" | |
"dummy" | |
"another\n" | |
"line" | |
"ends" | |
"now\n" | |
"or not?" | |
11:02:51 Temp$ |
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
require 'pp' | |
class Object | |
# Nest arbitrary methods which receive a block each | |
# argument list must at least contain a single mehtod | |
# name. | |
def nest_each(meth, *a, &b) | |
if a.empty? | |
send(meth, &b) | |
else | |
send(meth) do |x| | |
x.nest_each(*a, &b) | |
end | |
end | |
self | |
end | |
end | |
structure = [ | |
[ | |
"foo\n", "bar", | |
"dummy", | |
], | |
[ | |
"another\nline", | |
"ends", | |
"now\nor not?", | |
], | |
] | |
pp structure | |
print "\nNow the test...\n\n" | |
structure.nest_each :each, :each, :each_line do |line| | |
p line | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment