I am a Ruby beginner. I am confused by an example from Why's Poignant Guide.
I understand the following example's use (also from the Poignant Guide) of picks
in initialize
, because it is passed in as an argument.
class LotteryTicket
NUMERIC_RANGE = 1..25
attr_reader :picks, :purchased
def initialize( *picks )
if picks.length != 3
raise ArgumentError, "three numbers must be picked"
elsif picks.uniq.length != 3
raise ArgumentError, "the three picks must be different numbers"
elsif picks.detect { |p| not NUMERIC_RANGE === p }
raise ArgumentError, "the three picks must be numbers between 1 and 25"
end
@picks = picks
@purchased = Time.now
end
end
But how does initialize
in the following example start using picks
without picks
being passed in as an argument? Here, note1, note2, note3
is passed in instead. How does that ever get assigned to picks
?
class AnimalLottoTicket
# A list of valid notes.
NOTES = [:Ab, :A, :Bb, :B, :C, :Db, :D, :Eb, :E, :F, :Gb, :G]
# Stores the three picked notes and a purchase date.
attr_reader :picks, :purchased
# Creates a new ticket from three chosen notes. The three notes
# must be unique notes.
def initialize( note1, note2, note3 )
if [note1, note2, note3].uniq!
raise ArgumentError, "the three picks must be different notes"
elsif picks.detect { |p| not NOTES.include? p }
raise ArgumentError, "the three picks must be notes in the chromatic scale."
end
@picks = picks
@purchased = Time.now
end
end