Skip to content

Instantly share code, notes, and snippets.

@chrismdp
Created October 3, 2018 14:34
Show Gist options
  • Save chrismdp/2de1dc58adbd441ca2427d83b4a11f94 to your computer and use it in GitHub Desktop.
Save chrismdp/2de1dc58adbd441ca2427d83b4a11f94 to your computer and use it in GitHub Desktop.
Plus / Sandwich solution
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