Ruby初心者です。Why's Poignant Guide の例に混乱しています。
引数として渡されるため、次の例のpicks
inの使用 (これも心に訴えるガイドから) を理解しています。initialize
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
initialize
しかし、次の例では、引数として渡されpicks
ずに使用を開始するにはどうすればよいでしょうか? picks
ここでnote1, note2, note3
は、代わりに渡されます。それはどのようにしてに割り当てられ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