Chomp behaviour

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?

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.

echo "abc" outputs “abc\n”, try this instead

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

Roy

On Sat, Apr 16, 2011 at 11:08:10AM +0900, Steel S. wrote:

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


Posted via http://www.ruby-forum.com/.


/ She’s the kind of girl who climbed the ladder of success wrong by
| wrong. |
| |
\ – Mae West /

   \   ,__,
    \  (oo)____
       (__)    )\
          ||--|| *

On Sat, 16 Apr 2011 11:08:10 +0900, Steel S. wrote:

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).

‘|’ means in programming ‘or’ so the result would be.

if “abc” is true which it is then command line will not see

ruby -e ‘puts gets.chomp(“c”)’

----- Original Message -----
From: “Peter Z.” [email protected]
To: “ruby-talk ML” [email protected]
Sent: Friday, April 15, 2011 7:21:24 PM
Subject: Re: chomp behaviour

On Sat, 16 Apr 2011 11:08:10 +0900, Steel S. wrote:

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).

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

On 2011-04-15, at 10:08 PM, Steel S. wrote:

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


Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

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

iEYEARECAAYFAk2o/YcACgkQnsTBwAWZE9rYcACggPbZQh7j9njFyHz7Ri578l4I
nBcAn3OifIruAxP6kfKvikstClASfLFD
=lwkT
-----END PGP SIGNATURE-----

You are obviously not a *nix user ;]

That is a pipe, not a logical or.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs