# Already stalled in Pick Axe book - could use some help

**URL:** <https://www.ruby-forum.com/t/already-stalled-in-pick-axe-book-could-use-some-help/67395>\
**Category:** Ruby\
**Created:** [September 3, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/already-stalled-in-pick-axe-book-could-use-some-help/67395 "2006-09-03T18:15:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wirelesspower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58956e/32.png) [@wirelesspower](https://www.ruby-forum.com/u/wirelesspower)\
**Post date:** [September 3, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/already-stalled-in-pick-axe-book-could-use-some-help/67395/1 "2006-09-03T18:15:54Z")

</div>

Hi, total noob here. I’m only on page 25 of the Pick Axe book and I’m a  
little stuck:

I’m running Ruby version 1.8.4 on Windows XP Home Edition. I’m entering  
code via irb.

Here’s the problem: I enter the following code from the book:

class Song  
def initialize(name, artist, duration)  
@name = name  
@artist = artist  
@duration = duration  
end

def to\_s  
“Song: #@name–#@artist (#@duration)”  
end  
end

I successfully load the code using a file called song.rb here:

irb(main):008:0\> load “c:/code/song.rb”  
=\> true

Now the book states that if I type in this:  
song = Song.new(“Bicylops”, “Fleck”, 260)  
song.to\_s

I should see this:

“Song: BicylopsFleck(260)”

But I’m not, I’m getting this:

irb(main):013:0\> song = Song.new(“Bicylops”, “Fleck”, 260)  
=\> #\<Song:0x2c80440 @duration=260, @name=“Bicylops”, @artist=“Fleck”\>  
irb(main):014:0\> song.to\_s  
=\> “#Song:0x2c80440”

Obviously I’m still seeing the object ID when I should be seeing the  
string. Any help will be appreciated.

Regards,  
Jeff

---

<div class="post-metadata">

**Author:** ![wirelesspower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58956e/32.png) [@wirelesspower](https://www.ruby-forum.com/u/wirelesspower)\
**Post date:** [September 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/already-stalled-in-pick-axe-book-could-use-some-help/67395/2 "2006-09-03T18:59:52Z")

</div>

Jeff Rohrer wrote:

> Hi, total noob here. I’m only on page 25 of the Pick Axe book and I’m a  
> little stuck:
> 
> I’m running Ruby version 1.8.4 on Windows XP Home Edition. I’m entering  
> code via irb.
> 
> Here’s the problem: I enter the following code from the book:
> 
> irb(main):008:0\> load “c:/code/song.rb”  
> =\> true

Your code seems to be fine–however, you mention using  
irb but also, obviously, #load a file here. If the file  
contains the old code and you #load it after you have  
entered the new class code in irb, it will redefine your  
methods.

Try and just enter the above class definition either in  
irb or the file (and #load it in irb), but not both and  
see if that helps.

> 

> Regards,  
> Jeff

---

<div class="post-metadata">

**Author:** ![wirelesspower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58956e/32.png) [@wirelesspower](https://www.ruby-forum.com/u/wirelesspower)\
**Post date:** [September 3, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/already-stalled-in-pick-axe-book-could-use-some-help/67395/3 "2006-09-03T19:48:18Z")

</div>

> Try and just enter the above class definition either in  
> irb or the file (and #load it in irb), but not both and  
> see if that helps.
> 
> >

Got it. Thanks! That works, now I can move on:

C:\>irb  
irb(main):001:0\> class Song  
irb(main):002:1\> def initialize(name, artist, duration)  
irb(main):003:2\> @name = name  
irb(main):004:2\> @artist = artist  
irb(main):005:2\> @duration = duration  
irb(main):006:2\> end  
irb(main):007:1\> def to\_s  
irb(main):008:2\> “Song: #@name–#@artist (#@duration)”  
irb(main):009:2\> end  
irb(main):011:0\> song = Song.new(“Bicyclops”, “Fleck”, 260)  
=\> #\<Song:0x2c8bc78 @duration=260, @name=“Bicyclops”, @artist=“Fleck”\>  
irb(main):012:0\> song.to\_s  
=\> “Song: Bicyclops–Fleck (260)”

---

<div class="post-metadata">

**Author:** ![wirelesspower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58956e/32.png) [@wirelesspower](https://www.ruby-forum.com/u/wirelesspower)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/already-stalled-in-pick-axe-book-could-use-some-help/67395/4 "2006-09-04T14:43:53Z")

</div>

On 9/3/06, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > class Song
> 
> irb(main):003:0\> song.to\_s  
> =\> “Song: Bicylops–Fleck (260)”  
> irb(main):004:0\>
> 
> I think you may have something different in your actual source code.

Exactly  
maybe the lines

end # of initialize  
def to\_s

are missing

HTH  
Robert

> –  
> James B.
> 
> “Simplicity of the language is not what matters, but  
> simplicity of use.”
> 
> - Richard A. O’Keefe in squeak-dev mailing list

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![wirelesspower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58956e/32.png) [@wirelesspower](https://www.ruby-forum.com/u/wirelesspower)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/already-stalled-in-pick-axe-book-could-use-some-help/67395/5 "2006-09-04T14:43:53Z")

</div>

Jeff Rohrer wrote:

> ```
> @name = name
> @artist = artist
> @duration = duration
> 
> ```
> 
> end
> 
> def to\_s  
> “Song: #@name–#@artist (#@duration)”  
> end  
> end

I just tried this on WinXP, ruby 1.8.4 (2005-12-24) [i386-mswin32]

demo.rb has the code you provided.

d:\tmp\>irb  
irb(main):001:0\> load ‘demo.rb’  
=\> true  
irb(main):002:0\> song = Song.new(“Bicylops”, “Fleck”, 260)  
=\> #\<Song:0x2b1a370 @name=“Bicylops”, @duration=260, @artist=“Fleck”\>  
irb(main):003:0\> song.to\_s  
=\> “Song: Bicylops–Fleck (260)”  
irb(main):004:0\>

I think you may have something different in your actual source code.

–  
James B.

“Simplicity of the language is not what matters, but  
simplicity of use.”

- Richard A. O’Keefe in squeak-dev mailing list

---

<div class="post-metadata">

**Author:** ![wirelesspower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58956e/32.png) [@wirelesspower](https://www.ruby-forum.com/u/wirelesspower)\
**Post date:** [September 4, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/already-stalled-in-pick-axe-book-could-use-some-help/67395/6 "2006-09-04T14:44:06Z")

</div>

Jeff Rohrer wrote:

> irb(main):002:1\> def initialize(name, artist, duration)  
> =\> “Song: Bicyclops–Fleck (260)”  
> It may be time for you to create a Ruby source file. “irb” is a very  
> useful  
> tool, but once a program becomes as complex as the one you are playing  
> with, you have more than crossed the threshold of needing a source file.

---

<div class="post-metadata">

**Author:** ![wirelesspower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58956e/32.png) [@wirelesspower](https://www.ruby-forum.com/u/wirelesspower)\
**Post date:** [September 4, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/already-stalled-in-pick-axe-book-could-use-some-help/67395/7 "2006-09-04T14:44:18Z")

</div>

Jeff Rohrer wrote:

> Hi, total noob here. I’m only on page 25 of the Pick Axe book and I’m a  
> little stuck:  
> …snip  
> def to\_s  
> “Song: #@name–#@artist (#@duration)”  
> …  
> You forgot the curly braces, should be  
> “Song: #{@name}–#{@artist} (#{@duration})”

Cheers

---

<div class="post-metadata">

**Author:** ![wirelesspower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58956e/32.png) [@wirelesspower](https://www.ruby-forum.com/u/wirelesspower)\
**Post date:** [September 4, 2006, 4:55pm UTC](https://www.ruby-forum.com/t/already-stalled-in-pick-axe-book-could-use-some-help/67395/8 "2006-09-04T16:55:40Z")

</div>

ChrisH wrote:

> Jeff Rohrer wrote:
> 
> > def to\_s  
> > “Song: #@name–#@artist (#@duration)”  
> > …  
> > You forgot the curly braces, should be  
> > “Song: #{@name}–#{@artist} (#{@duration})”

The curly braces are not needed for class, instance, or global  
variables. (And maybe some other situations I don’t know about.) Jeff’s  
original code is just fine.
