chomp behaviour

S

Steel Steel

Using 1.9.1
I noticed something with chomp

$ echo "abc" | ruby -e 'puts gets.chomp("c")'
abc

$ irb
irb(main):001:0> "abc".chomp("c")
=> "ab"

It doesn't work on the command line, or am i doing it wrong.?
comments?
 
J

James

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

What are you trying to accomplish? Bear in mind that, on the command line,
you have bash or cmd intercepting everything. It's not the same as trying
things inside irb.


Look up what string.chomp(arg) does in irb vs the same thing on the command
line.
 
R

Roy Zuo

``echo "abc"`` outputs "abc\n", try this instead

echo -n abc | ruby -e "puts gets.chomp 'c'"

Roy

Using 1.9.1
I noticed something with chomp

$ echo "abc" | ruby -e 'puts gets.chomp("c")'
abc

$ irb
irb(main):001:0> "abc".chomp("c")
=> "ab"

It doesn't work on the command line, or am i doing it wrong.?
comments?

--
___________________________________________________________________
/ She's the kind of girl who climbed the ladder of success wrong by \
| wrong. |
| |
\ -- Mae West /
 
P

Peter Zotov

Using 1.9.1
I noticed something with chomp

$ echo "abc" | ruby -e 'puts gets.chomp("c")'
abc

$ irb
irb(main):001:0> "abc".chomp("c")
=> "ab"

It doesn't work on the command line, or am i doing it wrong.?
comments?

"gets" returns the whole read string, with trailing newline ("\n").
Try: echo "abc" | ruby -e 'p gets' # => "abc\n"

You can either chomp it twice, or do "echo -n" instead of "echo"
(the former does not add a newline), or use "strip" method, which
removes all whitespace from begin and end of a string (check also
"rstrip" and "lstrip" functions).
 
M

Mike Stok

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Using 1.9.1
I noticed something with chomp
=20
$ echo "abc" | ruby -e 'puts gets.chomp("c")'
abc
=20
$ irb
irb(main):001:0> "abc".chomp("c")
=3D> "ab"
=20
It doesn't work on the command line, or am i doing it wrong.?
comments?

gets includes the new line (if present):

ratdog:~ mike$ echo "abc" | ruby -e 'puts gets.chomp("c")'
abc
ratdog:~ mike$ echo -n "abc" | ruby -e 'puts gets.chomp("c")'
ab

Hope this helps,

Mike


- --=20

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)

iEYEARECAAYFAk2o/YcACgkQnsTBwAWZE9rYcACggPbZQh7j9njFyHz7Ri578l4I
nBcAn3OifIruAxP6kfKvikstClASfLFD
=3DlwkT
-----END PGP SIGNATURE-----
 

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

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top