Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created July 1, 2010 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rklemme/459749 to your computer and use it in GitHub Desktop.
Save rklemme/459749 to your computer and use it in GitHub Desktop.
Toy example of simpler loop nesting
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$
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