Functional Ruby Idioms
Method Chaining
Method chaining passes each result into the next method call.
Method Chaining
method_chaining.rb
raw_text =
cleaned = raw_text.strip
normalized = cleaned.downcase
title = normalized.capitalize
label = title.then { |text| "Topic: #{text}" }
puts "raw=#{raw_text}"
puts "cleaned=#{cleaned}"
puts "label=#{label}"
raw_text =
cleaned = raw_text.strip
normalized = cleaned.downcase
title = normalized.capitalize
label = title.then { |text| "Topic: #{text}" }
puts "raw=#{raw_text}"
puts "cleaned=#{cleaned}"
puts "label=#{label}"
raw_text =
cleaned = raw_text.strip
normalized = cleaned.downcase
title = normalized.capitalize
label = title.then { |text| "Topic: #{text}" }
puts "raw=#{raw_text}"
puts "cleaned=#{cleaned}"
puts "label=#{label}"
method chain
A method chain reads left to right when each step returns a value for the next step.