Jano Svitok
9/20/2006 11:34:00 AM
On 9/20/06, Vince /. <totalharmonicdistortion@hotmail.com> wrote:
> Hi everyone,
>
> Can anyone tell me why this doesnt work?
>
> Im getting this error
> cf.rb:26: undefined method `price' for #<Ticket:0x27ed554
> @d="11/12/2006", @price=5.0, @v="yer"> (NoMethodError)
>
> thanks, just learning :-)
>
>
> class Ticket
>
> def initialize(venue,date)
> @v = venue
> @d = date
> end
>
> def venue
> @v
> end
>
> def date
> @d
> end
>
> def price=(amount)
> @price = amount
> end
> end
>
> ticket = Ticket.new("yer","11/12/2006")
> puts "ticket stuff is #{ticket.venue} #{ticket.date}"
>
> ticket.price = (5.00)
>
> puts "ticket mooch is #{ticket.price}"
You need to define attribute reader:
def price
@price
end
BTW, you could accomplish the same by using
attr_reader(R)/attr_writer(W)/attr_accessor(RW):
(if you'd rename @v, @d to @venue, @date respectively)
> class Ticket
>
> def initialize(venue,date)
> @venue, @date = venue, date
> end
attr_reader :venue, :date # this will create venue() and date()
attr_accessor :price # this will create price=() and price()
> end