Let’s say I have this code snippet running.
class Song
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
def to_s
"Song: #{@name}--#{@artist} (#{@duration})"
end
end
SongA = Song.new("Bicyclops", "Fleck", 260)
puts SongA.to_s
If I replace SongA = Song.new("Bicyclops", "Fleck", 260) with SongA = Song.new("Bicyclops", "Fleck"), I get an error. Is this normal according to Ruby code construct?
Btw, I got the example from here. But I’m having trouble finding even after browsing through this doc. Thanks in advance for any resources you point me towards.
If your function definition does not specify a default value for the input parameters, you must supply them.
You could then initialize it omitting the properties for which you defined default values.
And you don’t need to initialize all the class properties in the
initialize()either. They can be added and initialized in other methods