On Tue, Jan 29, 2013 at 1:19 PM, Joel Pearson <[email protected]> wrote:
> Ryan Davis wrote in post #1094171:
>> This modifies the argument coming in. Don't ever call downcase! or other
>> mutating methods on an argument or you'll wind up in debugging hell.
>> Make a copy instead:
>>
>> string = string.downcase
>
> Ah, I didn't know that a bang method would also change the argument
> outside of the current scope as well! Dangerous.

That's why there is the exclamation mark in the first place.  It means
"potentially dangerous method" (defined by Matz).

Btw, this does not have that much to do with scope but it's rather
which object gets changed.  All places in code which reference that
particular instance will notice the change once they use the object.

> irb(main):001:0> a = 'a'
> => "a"
> irb(main):002:0> def t1(b)
> irb(main):003:1> b.upcase
> irb(main):004:1> end
> => nil
> irb(main):005:0> def t2(b)
> irb(main):006:1> b.upcase!
> irb(main):007:1> end
> => nil
> irb(main):008:0> t1 a
> => "A"
> irb(main):009:0> a
> => "a"
> irb(main):010:0> t2 a
> => "A"
> irb(main):011:0> a
> => "A"

Yeah, String methods with exclamation mark typically change the
instance itself whereas the "less dangerous" brothers typically return
a modified instance.

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- 
[email protected] | 
https://groups.google.com/d/forum/ruby-talk-google?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"ruby-talk-google" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to