On Tue, Oct 08, 2002 at 06:07:09PM -0700, Larry Wall wrote:
> I've always wondered what the ! postfix operator means.  The mathematicians
> think they know.   :-)

The Ruby folks think they know.  They're method name conventions.

>From "Programming Ruby"

    Methods that act as queries are often named with a trailing '?', such
    as instance_of?.  Methods that are 'dangerous' or modify the receiver,
    might be named with a trailing '!'.  For instance, String provides
    both a chop and a chop!.  The first one returns a modified string;
    the second modifies the receiver in place.  '?' and '!' are the only
    weird characters allowed as method name suffixes.

So...

    sorted_array = array.sort   # return a sorted array
    array.sort!                 # sort in place
    is_sorted = array.sorted?   # return true if the array is sorted

Interestingly enough, Ruby also supports the ?: operator as Perl does and
does not require whitespace between the tokens.

     $foo=1?42:0;   # $foo will have 42 in it.

I believe that ? simply binds tighter to method names than the ?:
operator.

This is fine:

    print defined? $foo;

This is a syntax error:

    print defined? $foo : 42;

    /home/schwern/tmp/foo.rb:1: parse error
    print defined? $foo : 42;
                         ^


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl Quality Assurance      <[EMAIL PROTECTED]>         Kwalitee Is Job One
Cottleston, Cottleston, Cottleston Pie.
Why does a chicken, I don't know why.
Ask me a riddle and I reply:
"Cottleston, Cottleston, Cottleston Pie."

Reply via email to