SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    undefined method

    i have a code here
    Code:
    class Song
    	def initialize(name, artist, duration)
    		@name = name
    		@artist = artist
    		@duration = duration
    	end
    	
    	attr_writer :duration
    end
    
    song = Song.new("Bicyclops", "Fleck", 260)
    song.duration
    song.duration = 257
    song.duration
    when i execute this using the console or the irb, i get this error

    NoMethodError: undefined method `duration' for #<Song:0x2838dc4>
    from (irb):17
    from :0


    i really dont get it...please help.thanks in advance.

  2. #2
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    attr_writer :duration only creates the duration= method.
    attr_reader :uration creates the duration method
    attr_accessor creates both.

    In your case, you want to use attr_accessor since you read and write.
    Ohai!

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah...but how can i get the output of the code above if i don't even know what's causing the error..i just copied the code from the book.thanks.

  4. #4
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, slipped my eyes with your solution...i changed it to attr_accessor and now it is working...thanks a lot.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •