Classes
โ Report an issue with this lessonclass Course
attr_reader :title, :price
def initialize(title, price)
@title = title
@price = price
end
def describe
"#{@title} -- $#{@price}"
end
end
course = Course.new("Ruby Mastery", 149.99)
puts course.describe
attr_reader generates getter methods automatically --
without it, you'd write def title; @title; end by hand for
every field.
Try it yourself
Exercise: Add attr_reader for title and price, then create a Course and print its describe.
Expected output:
Ruby Mastery -- $149.99
Run your code and get it working before marking this lesson complete.
// that was the last free lesson
9 more lessons โ including Capstone project: a course catalog โ plus a certificate are waiting.
Unlock the full course โ $149.99