[#1026] Is this a bug? — Dave Thomas <Dave@...>

18 messages 2000/01/03

[#1084] Infinite loop — Dave Thomas <Dave@...>

17 messages 2000/01/11

[#1104] The value of while... — Dave Thomas <Dave@...>

24 messages 2000/01/11

[ruby-talk:01203] Re: Semantics of chomp/chop

From: matz@... (Yukihiro Matsumoto)
Date: 2000-01-31 01:24:25 UTC
List: ruby-talk #1203
Hi,

In message "[ruby-talk:01195] Re: Semantics of chomp/chop"
    on 00/01/28, Dave Thomas <Dave@thomases.com> writes:

|I was wondering why copy it at all, as all four methods are allowed to 
|alter $_.

They are different.  For exapmle:

  foo = $_ = "foo\n"
  chomp
  p foo  #=> "foo\n"  -- unchanged
  p $_   #=> "foo"

  foo = $_ = "foo\n"
  chomp!
  p foo  #=> "foo"
  p $_   #=> "foo"

See?  Non-bang versions leave original strings unchanged.

|I don't know if it's necessary, but you _could_ have [g]sub! and
|cho[m]p! always return the string, and set $& to whatever was
|changed. If $& is nil, then no change was made.

It may be better to return strings always.  But I think `$&' is not
sufficient, because it is for `match', not `modifys'.

							matz.

In This Thread