class Recipe def initialize ingredients=[] @ingredients = ingredients end endbecause sending the message then is as awful as:
Recipe.new ['Banana', 'Chocolate', 'Almonds']or even worse:
Recipe.new ['Banana']But the splat (*) operator in Ruby can solve it. The code can be refactored to:
class Recipe def initialize *ingredients @ingredients = ingredients.flatten.sort end def ingredients @ingredients.join(', ') end endand sending the message again:
recipe = Recipe.new 'Banana', 'Chocolate', 'Almonds' recipe.ingredients => "Almonds, Banana, Chocolate"or assigning just one object:
Recipe.new 'Banana'Even assigning an Array works, because Array#flatten flattens the collection into an one dimensional array:
ingredients = %w(Banana Chocolate Almonds) recipe = Recipe.new ingredients => #<Recipe:0x00000003de66a8 @ingredients=["Almonds", "Banana", "Chocolate"]>In general the splat (*) operator can be used to split an Array:
one, two, three = *[1, 2, 3] one => 1 two => 2 three => 3and to collect an Array:
*numbers = 1, 2, 3 numbers => [1, 2, 3]With the help of the Ruby splat (*) operator method parameters can be variable from zero up to many and handled as an Array internally. No archaic Array assignments anymore. And flatten the collection parameter with Array#flatten increases the flexibility of the API method.
Further articles of interest:
- Remove boolean parameters from your Ruby API!
- Reduce Ruby method parameter complexity!
- Chain your Ruby methods!
Supported by Ruby 2.1.1
Keine Kommentare:
Kommentar veröffentlichen