Skip to content

Instantly share code, notes, and snippets.

@joegiralt
Created June 10, 2013 22:08
Show Gist options
  • Save joegiralt/5752841 to your computer and use it in GitHub Desktop.
Save joegiralt/5752841 to your computer and use it in GitHub Desktop.
method chaining
# Chain at least 5 method calls to an object. Reduce this operation one chain at a time.
# Don't just use the methods below, make up your own chain and try to see if you can do something fun with Ruby like in the example.
# Example:
# puts "start".reverse.slice(0,2).concat("uth").capitalize
# # => Truth
# Because:
# puts "start".reverse
# #=> trats
# puts "start".reverse.slice(0,2)
# #=> tr
# puts "start".reverse.slice(0,2).concat("uth")
# #=> truth
# puts "start".reverse.slice(0,2).concat("uth").capitalize
# #=> Truth
# At any given point in the chain, there is an evaluation that then receives the next method call.
puts "racecar".upcase
puts "racecar".upcase.slice(0..3)
puts "racecar".upcase.slice(0..3).reverse
puts "racecar".upcase.slice(0..3).reverse.concat(" RAC")
puts "racecar".upcase.slice(0..3).reverse.concat(" RAC").reverse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment