[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newbie with a weird technical problem (@ least I think it's weird

will

12/27/2006 1:38:00 PM

Yep, as the post title implies, I'm a newbie. Being a pragmatic newbie
I'd rather be learning to program, than searching for answers to
non-language acquisition questions/problems. I'm pretty sure the
following falls in that latter group, so if it's in a manual somewhere
please tell me so _politely_ and point me to _the_ manual. :-)

I'm starting to go through Dave Thomas' Programming Ruby/Picaxe (I'm on
pgs 25-26 2nd edition if you care) and I'm using TextMate/Terminal to
code and run modified versions of the book's examples. So I ran a
version of the following with a typographical error, which was apparent
with the Terminal feedback. So far, so good. I fixed the error and
tried to run it again. The problem is that now when I run it, all I
get in response is a new blank prompt. I've tried renaming the file,
the class, giving it a completely new set of variables in the array and
quiting/restarting the Terminal. If I type the _exact_ same thing into
irb I'm able to create a book and book2 instance and .inspect each of
them. But with the TextMate/Terminal combo = Nothing, nadda, zip!
HELP, PLEASE!


#filename bib.rb

class Book
def initialize(title, author, pages)
@title = title
@author = author
@pages = pages
end
end

book = Book.new("Ruby Tutorial", "Dave", 831)
book.inspect


#terminal results
computer: me$ ruby bib.rb
computer: me$


9 Answers

Tim Hunter

12/27/2006 1:45:00 PM

0

will wrote:
> Yep, as the post title implies, I'm a newbie. Being a pragmatic newbie
> I'd rather be learning to program, than searching for answers to
> non-language acquisition questions/problems. I'm pretty sure the
> following falls in that latter group, so if it's in a manual somewhere
> please tell me so _politely_ and point me to _the_ manual. :-)
>
> I'm starting to go through Dave Thomas' Programming Ruby/Picaxe (I'm on
> pgs 25-26 2nd edition if you care) and I'm using TextMate/Terminal to
> code and run modified versions of the book's examples. So I ran a
> version of the following with a typographical error, which was apparent
> with the Terminal feedback. So far, so good. I fixed the error and
> tried to run it again. The problem is that now when I run it, all I
> get in response is a new blank prompt. I've tried renaming the file,
> the class, giving it a completely new set of variables in the array and
> quiting/restarting the Terminal. If I type the _exact_ same thing into
> irb I'm able to create a book and book2 instance and .inspect each of
> them. But with the TextMate/Terminal combo = Nothing, nadda, zip!
> HELP, PLEASE!
>
>
> #filename bib.rb
>
> class Book
> def initialize(title, author, pages)
> @title = title
> @author = author
> @pages = pages
> end
> end
>
> book = Book.new("Ruby Tutorial", "Dave", 831)
> book.inspect
>
>
> #terminal results
> computer: me$ ruby bib.rb
> computer: me$
>
>
>

It's a language question. The inspect method returns a string. That
string is not automatically displayed on the terminal. You have to print
the string if you want to see it. Use "puts book.inspect" for example.

The reason you see the result in irb is that irb prints it for you.

Rimantas Liubertas

12/27/2006 1:46:00 PM

0

> If I type the _exact_ same thing into
> irb I'm able to create a book and book2 instance and .inspect each of
> them. But with the TextMate/Terminal combo = Nothing, nadda, zip!
> HELP, PLEASE!
<...>

> book = Book.new("Ruby Tutorial", "Dave", 831)
> book.inspect

It is because you do not output anything.
Change the last line to puts book.inspect


Regards,
Rimantas
--
http://rim...

Dick Davies

12/27/2006 1:46:00 PM

0

On 27/12/06, will <will.graduate@gmail.com> wrote:


> If I type the _exact_ same thing into
> irb I'm able to create a book and book2 instance and .inspect each of
> them. But with the TextMate/Terminal combo = Nothing, nadda, zip!
> HELP, PLEASE!
>
>
> #filename bib.rb
>
> class Book
> def initialize(title, author, pages)
> @title = title
> @author = author
> @pages = pages
> end
> end
>
> book = Book.new("Ruby Tutorial", "Dave", 831)
> book.inspect
>
>
> #terminal results
> computer: me$ ruby bib.rb
> computer: me$

The reason you see output in irb is that irb shows you what
the last statement evaluated to, like this:

rasputnik@hypnotoad:~$ irb
irb(main):001:0> 1
=> 1
irb(main):002:0> quit


Object#inspect returns a string, but you aren't doing anything with it!
irb is showing you the value of book.inspect as usual, but the script
version is less verbose.

try

puts book.inspect

and you'll see output from the non-irb version.

--
Rasputin :: Jack of All Trades - Master of Nuns
http://number9.helloope...

will

12/27/2006 1:50:00 PM

0

Thank you! Your explanation helps me understand the subsequent
failures. Any idea why it 'almost' worked the first time? (it printed
the @pages and @author info, and then it told me there was an error.)


On Dec 27, 6:44 am, Timothy Hunter <TimHun...@nc.rr.com> wrote:
> will wrote:
> > Yep, as the post title implies, I'm a newbie. Being a pragmatic newbie
> > I'd rather be learning to program, than searching for answers to
> > non-language acquisition questions/problems. I'm pretty sure the
> > following falls in that latter group, so if it's in a manual somewhere
> > please tell me so _politely_ and point me to _the_ manual. :-)
>
> > I'm starting to go through Dave Thomas' Programming Ruby/Picaxe (I'm on
> > pgs 25-26 2nd edition if you care) and I'm using TextMate/Terminal to
> > code and run modified versions of the book's examples. So I ran a
> > version of the following with a typographical error, which was apparent
> > with the Terminal feedback. So far, so good. I fixed the error and
> > tried to run it again. The problem is that now when I run it, all I
> > get in response is a new blank prompt. I've tried renaming the file,
> > the class, giving it a completely new set of variables in the array and
> > quiting/restarting the Terminal. If I type the _exact_ same thing into
> > irb I'm able to create a book and book2 instance and .inspect each of
> > them. But with the TextMate/Terminal combo = Nothing, nadda, zip!
> > HELP, PLEASE!
>
> > #filename bib.rb
>
> > class Book
> > def initialize(title, author, pages)
> > @title = title
> > @author = author
> > @pages = pages
> > end
> > end
>
> > book = Book.new("Ruby Tutorial", "Dave", 831)
> > book.inspect
>
> > #terminal results
> > computer: me$ ruby bib.rb
> > computer: me$It's a language question. The inspect method returns a string. That
> string is not automatically displayed on the terminal. You have to print
> the string if you want to see it. Use "puts book.inspect" for example.
>
> The reason you see the result in irb is that irb prints it for you.


Morton Goldberg

12/27/2006 4:35:00 PM

0

On Dec 27, 2006, at 8:37 AM, will wrote:

> ... I'm using TextMate/Terminal to
> code and run modified versions of the book's examples.

Since you're using TextMate (as I do), I recommend running Ruby code
from within TextMate. No need to bother with Terminal.

1. Use Bundles>Ruby>Run to run your script. Output goes to a RubyMate
window which will pop up.

2. Try the following:

class Book
def initialize(title, author, pages)
@title = title
@author = author
@pages = pages
end
end

book = Book.new("Ruby Tutorial", "Dave", 831)
book.inspect # =>

Note the '# =>' added to the last line. Choose Ruby>Evaluate and
Update '# =>' Markers from the Bundles menu. The last line will
change to

book.inspect # => "#<Book:0x25454 @author=\"Dave\", @title=\"Ruby
Tutorial\", @pages=831>"

There are shortcuts for these bundle commands, and there are several
other aids to evaluating Ruby code built-in to the TextMate Ruby
bundle. I think you will find TextMate makes it easy to explore Ruby
without constantly switching back and forth to Terminal.

Regards, Morton

Gavin Kistner

12/27/2006 4:46:00 PM

0

will wrote:
> book = Book.new("Ruby Tutorial", "Dave", 831)
> book.inspect

Others have mentioned the need to output the value you are returning,
and have suggested:
puts book.inspect
as a way to see that information. I just wanted to add the suggestion
that the 'p' method:
p book
automatically calls #inspect on the object(s) passed, and then spits
them to stdout like puts does. (For comparison, the puts method calls
#to_s on the object(s) passed.) 'p' is very convenient when developing
and testing out your application, as the output sometimes gives you
more insight into the actual format of the data...particularly for
arrays:

irb(main):001:0> a = [ 1, '2', [ '3 4', 5 ], 6 ]
=> [1, "2", ["3 4", 5], 6]

irb(main):002:0> puts a
1
2
3 4
5
6
=> nil

irb(main):003:0> p a
[1, "2", ["3 4", 5], 6]
=> nil

As you can see from that first line, it's the result of #inspect that
irb uses when outputting the result of the last command. (Both puts and
p return nil when finished, which is where those two "=> nil" lines
come from in irb.)

JustDoIt

7/23/2010 12:17:00 AM

0

Walter Harding wrote

> No, Zepp, the thing is that you had a room full of NAACP member who
> cheered when the speaker bragged about denying a family farm a long
> because he was white.
>

Rush said today that if we kept up with lynchings and segregation this never
would have happened. God dam fucking niggers, pretty soon there wont be
any left on the right and they can go on and vote for that nigger obama all
they want. Brietbart says the NAACP are nothing but a bunch of subhuman
racist apes who belong in Africa. Ever noticed how Fox News don't have no
niggers on anymore? Since niggers don't vote GOP, we should ship em back to
Africa where they belong. All they want to do is steal our stuff, fuck our
daughters and steal our jobs.

Hitler was right, so was Hannity and so was Hal Turner.

A nigger in the white house has led to the predictable result of total
disaster. What do you expect with a nigger president?

What we need is a WHITE man in the WHITE house. If only a white man were
there now, all would be well and we would all be rich and healthy, and
winning wars right and left. A WHITE man would lead us all to paradise
and prosperity. A white president is what America needs, wants and
cannot do without! How long must we suffer without a white president?

Why can't the white man be given a chance? Why must we live under the
dictatorship of a nigger? A WHITE man will lead us out of this mess,
because WHITE men are virtuous and intelligent. Unlike this crazy
nigger in there now!

The white man must finally be given a chance to govern us in a virtuous
and most importantly, WHITE manner.

A white man will save us all!

sillapond

7/23/2010 4:28:00 PM

0

On 07/22/2010 05:03 PM, Joe Cool wrote:
> You need to listen to the tape again.

Start at 24:00

"Some of the racism we though was buried, now didn't it surface?"

"Now we endured 8 years of the Bushes and we didn't do the stuff these
Republicans are doing because we have a black President.."

Nice one Shirley, blame Republicans at large, very bigoted of you...

Indy Vidual

7/23/2010 10:40:00 PM

0

sillapond wrote:
> On 07/22/2010 05:03 PM, Joe Cool wrote:
>> You need to listen to the tape again.
>
> Start

Finish.

You are a little dick bitch who feels like it is his civic duty as
a Usenet troll to place his nose firmly in the sphincters of those he
dislikes every time they post. They all own you, spammy.

'Shit happens'
---Traitorous 'Spammy' Sam's reply to the fact that 34 Americans
died and 170 were injured when Israel attacked the USS Liberty.
Spammy is a gutless coward who has never served his country in
uniform.