Problem with 'chomp'

B

Barry O'gorman

Simple program:

while($_ != "q")
puts "Running..."
print "Enter q to quit: "
gets
chomp
end

However - getting the following in any progams in which I use 'chomp':
'undefined local variable or method 'chomp' for main: Object (NameError)
 
X

Xavier Noria

Simple program:

while($_ !=3D "q")
=C2=A0 =C2=A0puts "Running..."
=C2=A0 =C2=A0print "Enter q to quit: "
=C2=A0 =C2=A0gets
=C2=A0 =C2=A0chomp
end

However - getting the following in any progams in which I use 'chomp':
'undefined local variable or method 'chomp' for main: Object (NameError)

chomp, gsub, and others can only be used without a receiver in
one-liners. They are thought for making -p loops and such short.

In regular Ruby you normally use explicit variables (and in regular
Perl you normally also do).
 
S

Sven Schott

[Note: parts of this message were removed to make it a legal post.]

Really? I'm using ruby 1.8.7 (on a mac) and the code runs fine for me. Not
that I disagree with the comment though: I think the receiver should be used
for clarity.

btw, runs fine in irb and ruby proper.
 
C

Christopher Dicely

Simple program:

while($_ !=3D "q")
=C2=A0 =C2=A0puts "Running..."
=C2=A0 =C2=A0print "Enter q to quit: "
=C2=A0 =C2=A0gets
=C2=A0 =C2=A0chomp
end

However - getting the following in any progams in which I use 'chomp':
'undefined local variable or method 'chomp' for main: Object (NameError)

At least in Ruby 1.9, Kernel#chomp only exists when the -n or -p
command-line options are used when launching ruby, and mostly would
only be used when you are putting the code all on the ruby command
line. If you want to take lines from the programs input, chomp them,
and send them (now run together) to the output until you hit a "q" by
itself in the input, this should do the trick:

ruby -p -e "chomp; $_ =3D=3D 'q' ? break : $_"
 
X

Xavier Noria

Really? I'm using ruby 1.8.7 (on a mac) and the code runs fine for me. Not
that I disagree with the comment though: I think the receiver should be used
for clarity.

Yeah, it's new in 1.9.
 
S

Sven Schott

[Note: parts of this message were removed to make it a legal post.]

Slightly offtopic, but is everyone using 1.9? Are my marbles rolling away
because I'm still on 1.8?

Only on 1.8 because I thought that was still the official stable version.
(i.e. won't break anything)
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Slightly offtopic, but is everyone using 1.9? Are my marbles rolling away
because I'm still on 1.8?

Only on 1.8 because I thought that was still the official stable version.
(i.e. won't break anything)

I use 1.8.7 on my Macbook Pro, running Snow Leopard, because all other
versions are painfully slow (maybe an rvm problem?)
I use 1.9.1 on my Hackintosh desktop, running Leopard, it is not slow there,
so I use it.

But I always reference the 1.8.6 docs anyway, so hypothetically anything I
write should be good for either of them, unless a gem has issues.

All my friends are on 1.9.1, Heroku is on 1.8.7, at my work it was a mix
between 1.8.6 and 1.8.7
 
R

Robert Klemme

Slightly offtopic, but is everyone using 1.9? Are my marbles rolling away
because I'm still on 1.8?

Yes. And you will die.

Seriously: I think we are still in a transitional phase. It does seem
though that most people coming to the language now start out 1.9ish.
Applications which are around for a while are likely to still use 1.8
(or even 1.8.6).
Only on 1.8 because I thought that was still the official stable version.
(i.e. won't break anything)

Currently I'd say if robustness is your main concern that is probably
the best choice. And I guess that will be true for a while (maybe a
year or so) although my crystal ball rolled away yesterday so please
take it with a grain of salt. :)

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top