[TxMt] Re: problem running ruby code...

Chris Rebert textmate at rebertia.com
Fri Jun 18 02:59:47 UTC 2010


On Thu, Jun 17, 2010 at 7:49 PM, plastichairdoo
<plastichairdoo at gmail.com> wrote:
> hi there - I've been trying my hand at learning Ruby and have been running some small scripts within TM but now I've run into a problem... here's the code I'm trying to run:
>
> ------------CODE--------------
>
> class Song
>  def initialize(name, artist, duration)
>    @name = name
>    @artist = artist
>    @duration = duration
>  end
>
> end
>
> song = Song.new("Ruby Tuesday", "Rolling Stones", 360)
> song.inspect
>
> ------------CODE--------------
>
> however when I use the "Run" command the output window shows up blank - no errors, just blank.
> adding this line to the class:
>
> printf("foo")
>
> results in "foo" in the output window but nothing else.
>
> this code is taken directly from the "Programming Ruby" book published by The Pragmatic Programmers and should output some stuff that would take me far too long to type...
>
> so, I'm obviously a bit of a noob - can you tell me what's going on??

.inspect just returns a String representing an object in a format
useful for debugging. To actually *output* the returned string, you
must pass it to puts(), printf(), or similar.

The example in the book is probably being input & run into IRB
(http://en.wikipedia.org/wiki/Interactive_Ruby_Shell ), which by
default automatically outputs the result of each expression evaluated
at the top-level; in this case, it automatically outputs the String
returned by the expression "song.inspect".

Long story short: The interactive shell works differently than the
vanilla ruby interpreter. Either add an explicit call to puts/printf,
or input & run the code in irb from the Terminal instead of using
TextMate.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the textmate mailing list