Created
October 3, 2018 14:34
-
-
Save chrismdp/2de1dc58adbd441ca2427d83b4a11f94 to your computer and use it in GitHub Desktop.
Plus / Sandwich solution
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
def plus(num) | |
(1..num).to_a.map {|x| yield(x) }.join | |
end | |
p plus(3) { "ding" } | |
p plus(3) { |x| "...#{x}" } | |
p plus(5) { |x| (x + 2).to_s } | |
def sandwich(layers = 1, &block) | |
if (layers == 0) | |
"bread" | |
else | |
[sandwich(layers - 1, &block), yield, "bread"].join(", ") | |
end | |
end | |
p sandwich { "meat" } | |
p sandwich(2) { "cheese" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment