3 Replies - 638 Views - Last Post: 26 September 2013 - 12:24 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

ruby error - no idea why

Posted 26 September 2013 - 10:30 AM

Hello there I am reading the Pragmatic Programmer's Guide on ruby-doc.com. I am following along with the examples in my text editor and am receiving an error with the following code. I would appreciate any assistance on this issue. Thank you very much for your time. My method for getting my code interpreted is to type Ruby, create a space and then drag my TextEdit File into terminal and hit return.The following is the code that I have written in TextEdit:

class Song
	def initialize(name, artist, duration)
		@name = name
		@artist = artist 
		@duration = duration
	end
end
aSong = Song.new(Sea Changes, Beck, 366)
aSong.inspect



The error for the code is as follows:

/Users/gregoryheaphyheaphy/Desktop/JUKEBOX.txt:8: uninitialized constant Changes (NameError)

In addition is there a way to call upon methods in terminal? I find that I have to call upon the methods within my script which seems weird to me.
Thanks

This post has been edited by xclite: 26 September 2013 - 10:48 AM
Reason for edit:: Adding code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: ruby error - no idea why

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 905
  • View blog
  • Posts: 3,167
  • Joined: 12-May 09

Re: ruby error - no idea why

Posted 26 September 2013 - 10:51 AM

A couple of things.

First, I think you meant to call like this:
aSong = Song.new("Sea Changes", "Beck", 366)

You can't just type in words and have them mean something - the error here is saying that Changes isn't an initialized constant. That's because typing Changes looks like a variable, and variables with capital letters are constants. You need to pass in a string, which is created by putting the words in quotes.

Next, you call aSong.inspect. If you run this as a ruby script (after fixing the above), you won't get any output. You need to print the string returned by inspect:
puts aSong.inspect

Finally, you should check out irb to see about using Ruby interactively.
Was This Post Helpful? 1
  • +
  • -

#3 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: ruby error - no idea why

Posted 26 September 2013 - 12:13 PM

Thank you very much. This was very helpful and I appreciate you feedback.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: ruby error - no idea why

Posted 26 September 2013 - 12:24 PM

View Postxclite, on 26 September 2013 - 07:51 PM, said:

puts aSong.inspect


Which can be written more concisely as p aSong.

As a style suggestion: The convention in Ruby is to use snake_case, not camelCase. So in idiomatic Ruby the variable would be called a_song. Or really just song - articles in variable names aren't very common.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1