If Statement Help and param passing

Thanks Ryan, no wonder I couldnt find any doco :slight_smile: doh ! , one more for the
day

I have noticed that some params in methods / function are passed as follows:

object.method(value, *data)

is the * character serving a purpose here, eg passing by value rather than
reference ?

Thanks for all your help …

Graeme Matthew
Analyst Programmer
Mercer Investment Consulting
Level 29, 101 Collins Street, Melbourne, VIC, 3001, Australia
Tel - 61 3 9245 5352 Fax - 61 3 9245 5330
visit http://www.merceric.com

···

-----Original Message-----
From: Ryan King [mailto:rking@panoptic.com]
Sent: Thursday, 19 September 2002 16:22
To: ruby-talk@ruby-lang.org
Subject: Re: If Statement Help

On 2002.09.19, Matthew, Graeme Graeme.Matthew@mercer.com wrote:

if File.name?

Can someone please explain the ? usage as I have searched all over the
Pragmatic Programmer pages with no answer.

That’s just a part of the method name. Run “irb” and paste this
into it:

class Monkey
def hasFleas?
true
end
end
class Rking
def hasFleas?
false
end
end
Monkey.new.hasFleas?
Rking.new.hasFleas?

You will find that Monkeys have fleas while Rking’s decidedly do
NOT.

Anyway, the point is: Putting a question mark at the end of a
method name is a custom to imply that the method returns a
boolean.

  • Ryan
    __

This e-mail and any attachments may be confidential or legally privileged.
If you received this message in error or are not the intended recipient, you
should destroy the e-mail message and any attachments or copies, and you are
prohibited from retaining, distributing, disclosing or using any information
contained herein. Please inform us of the erroneous delivery by return
e-mail.

Thank you for your cooperation.


ec03/04

This is the price we pay for using funny syntax here and there. =)

In this case (specifically, when * is used as a unary operator),
it is the “splat” operator. Or the “splatsterisk”, if you will.
(Most people won’t).

As an example:
def foo a, b
p a, b
end
foo *[‘don’, ‘key’] # Same as
foo ‘don’, ‘key’ # …this

That is, it peels off the arrayness of the argument. Kind of
funky, but it’s your only recourse sometimes.

And, in a preemptive strike, I’ll answer this one:

def bar first, *rest
p first, rest
end
bar 1, 2, 3, 4

In this case, “*” is serving as an “unsplat” (or “unsplatserisk”,
but almost no one will). It allows you to pass an arbitrary
number of arguments and they all get rolled into one array, in
this case “rest”. It’s like C’s varargs.

  • Ryan King

P.S.: Sorry for using cliche` metasyntactic method names. I’m
being lazy.

···

On 2002.09.19, Matthew, Graeme Graeme.Matthew@mercer.com wrote:

Thanks Ryan, no wonder I couldnt find any doco :slight_smile: doh ! , one more for the
day

I have noticed that some params in methods / function are passed as follows:

object.method(value, *data)

is the * character serving a purpose here, eg passing by value rather than
reference ?

“Matthew, Graeme” wrote:

I have noticed that some params in methods / function are passed as follows:

object.method(value, *data)

is the * character serving a purpose here, eg passing by value rather than
reference ?

It unpacks an Array, passing each element as a separate argument to the
method.

I.e.
object.method(value, a, b, c)
is equivalent to
data = [a, b, c]
object.method(value, *data)
if I recall correctly.

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)