Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2984 > unrolled thread

chomp behaviour

Started bySteel Steel <angel_steel@ymail.com>
First post2011-04-15 21:08 -0500
Last post2011-04-15 21:23 -0500
Articles 5 — 5 participants

Back to article view | Back to comp.lang.ruby


Contents

  chomp behaviour Steel Steel <angel_steel@ymail.com> - 2011-04-15 21:08 -0500
    Re: chomp behaviour James <oscartheduck@gmail.com> - 2011-04-15 21:15 -0500
    Re: chomp behaviour Roy Zuo <roylzuo@gmail.com> - 2011-04-15 21:19 -0500
    Re: chomp behaviour Peter Zotov <whitequark@whitequark.org> - 2011-04-15 21:21 -0500
    Re: chomp behaviour Mike Stok <mike@stok.ca> - 2011-04-15 21:23 -0500

#2984 — chomp behaviour

FromSteel Steel <angel_steel@ymail.com>
Date2011-04-15 21:08 -0500
Subjectchomp behaviour
Message-ID<fd6f7d3388641b7b1873dbce2346c695@ruby-forum.com>
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?

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

[toc] | [next] | [standalone]


#2985

FromJames <oscartheduck@gmail.com>
Date2011-04-15 21:15 -0500
Message-ID<BANLkTikvGAsWqPoEc+iOBauQYRBr+CKDeg@mail.gmail.com>
In reply to#2984
[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.

On Fri, Apr 15, 2011 at 8:08 PM, Steel Steel <angel_steel@ymail.com> wrote:

> 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?
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

[toc] | [prev] | [next] | [standalone]


#2986

FromRoy Zuo <roylzuo@gmail.com>
Date2011-04-15 21:19 -0500
Message-ID<20110416021847.GA17374@bender>
In reply to#2984
``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 Steel wrote:
> 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?
> 
> -- 
> 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)____
           (__)    )\
              ||--|| *

[toc] | [prev] | [next] | [standalone]


#2987

FromPeter Zotov <whitequark@whitequark.org>
Date2011-04-15 21:21 -0500
Message-ID<a0fe6e31a88686b5df86f345e2c1fb58@mail.whitequark.org>
In reply to#2984
 On Sat, 16 Apr 2011 11:08:10 +0900, Steel Steel wrote:
> 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).

-- 
   WBR, Peter Zotov.

[toc] | [prev] | [next] | [standalone]


#2988

FromMike Stok <mike@stok.ca>
Date2011-04-15 21:23 -0500
Message-ID<7CF8D59E-7FA5-4F7D-84F7-62E12578D5DB@stok.ca>
In reply to#2984
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


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

> 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 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 Stok <mike@stok.ca>
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-----

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web