[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Named/positional method args

darren kirby

6/23/2007 9:28:00 PM

I have a method here that takes two arguments. Both are optional, with
appropriate default values. The first is a string which represents a filename
to write data to. The second is an integer which represents which data from a
choice of 1 or more should be written to the file.

So I have:

> def write_picture(outfile = nil, n = 1)

When I call as:

> write_picture()

it does the right thing and uses both defaults.

When I call as:

> write_picture("somefile")

it does the right thing and writes the default data to "somefile"

When I call:

> write_picture("front cover", 1)
> write_picture("back cover", 2)

it does the right thing. However, when I do:

> write_picture(n=1)

I am sure you can guess what happens. I get a picture written to a file
with "1" as a filename. This seems like non-intuitive behavior. With no
support for named args shouldn't it error here?

Now: I gather I can do:

> def write_picture(*args)

and perhaps do a type check on each argument, as there must be one each String
and Integer, and sort them out appropriately. However, this relies too
heavily, for my comfort, on the users passing sane values to the method.

Is there a clean way to accomplish my goal? Are there plans to give named
arguments to Ruby in the future?

Thanks for consideration,
-d
--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

19 Answers

Tim Hunter

6/23/2007 9:35:00 PM

0

darren kirby wrote:
>> def write_picture(outfile = nil, n = 1)
>>
>
> Is there a clean way to accomplish my goal? Are there plans to give named
> arguments to Ruby in the future?
>

The standard technique for doing this is to use a hash with symbols for
the argument names. Remember Ruby collects up trailing hash assignments
into a single hash, so you could do something like this;

write_picture(:outfile=>whatever, :n=>1)

Your method is defined like this:

def write_picture(args)
outfile = args[:outfile]
n = args[:n]
...
end

And, yes, I believe the current plan is to have named arguments in a
future release of Ruby.

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


dblack

6/23/2007 9:36:00 PM

0

Gregory Brown

6/23/2007 9:39:00 PM

0

On 6/23/07, darren kirby <bulliver@badcomputer.org> wrote:

> it does the right thing. However, when I do:
>
> > write_picture(n=1)
>
> I am sure you can guess what happens. I get a picture written to a file
> with "1" as a filename. This seems like non-intuitive behavior. With no
> support for named args shouldn't it error here?

No, that just becomes write_picture(1)

the reason for this is that arguments can be expressions, not just
singular values.

> Now: I gather I can do:
>
> > def write_picture(*args)
>
> and perhaps do a type check on each argument, as there must be one each String
> and Integer, and sort them out appropriately. However, this relies too
> heavily, for my comfort, on the users passing sane values to the method.

> Is there a clean way to accomplish my goal?

Yes. use a hash

def my_method(options={})
a = options[:a]
b = options[:b]
end

my_method(:a => 1, :b => 2)
my_method(:b => 3)
my_method(:a => 1)

all work. You can then do something like

def my_method(options={})
a = options[:a] || 10
b = options[:b] || 5
end

to set defaults, or if you have a whole bunch, look at Hash#merge.

> Are there plans to give named arguments to Ruby in the future?

Not that I know of but syntactic sugar will be allowed for hash
definition, which will allow

my_method( :foo => 2, :bar => 10)

to become

my_method( :foo : 2, :bar : 10)

(I think...)

Gregory Brown

6/23/2007 9:48:00 PM

0

On 6/23/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:

> Not that I know of but syntactic sugar will be allowed for hash
> definition, which will allow
>
> my_method( :foo => 2, :bar => 10)
>
> to become
>
> my_method( :foo : 2, :bar : 10)

Oh, the new literal syntax is:

{ foo : 2, bar : 10 }

so that'd be my_method( foo : 2, bar : 10 )

But it's supposed to be an addition, not a replacement for the { :foo
=> 2 } syntax.

darren kirby

6/23/2007 10:07:00 PM

0

Thanks guys!

I've got it sorted now, using a hash.

So for my Rdoc, I take it I would want to have:

# Writes embedded images to a file
#
# :call-seq:
# write_picture() -> nil
# write_picture(:outfile=>"str") -> nil
# write_picture(:n=int) -> nil
# write_picture(:outfile="str", :n=int) -> nil

Is this right?

Thanks again,
-d
--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

darren kirby

6/23/2007 10:09:00 PM

0

*Sigh*, I mean:

# Writes embedded images to a file
#
# :call-seq:
# write_picture() -> nil
# write_picture(:outfile=>"str") -> nil
# write_picture(:n=>int) -> nil
# write_picture(:outfile=>"str", :n=>int) -> nil

-d
--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

Trans

6/24/2007 12:14:00 AM

0



On Jun 23, 6:09 pm, darren kirby <bulli...@badcomputer.org> wrote:
> *Sigh*, I mean:
>
> # Writes embedded images to a file
> #
> # :call-seq:
> # write_picture() -> nil
> # write_picture(:outfile=>"str") -> nil
> # write_picture(:n=>int) -> nil
> # write_picture(:outfile=>"str", :n=>int) -> nil
>
> -d

Bingo.

But hey, as Resident Dreamer, I can't help but wonder how cool it
might be to have highly descriptive and auto-flexible arguments:

def write_picture(n <= Integer, outfile <= String)
...
end

write_picture("front cover", 1)
write_picture(1, "front cover")

Sacrifice a little duck-typiness for some arg-flippiness :)

T.


Gregory Brown

6/24/2007 1:21:00 AM

0

On 6/23/07, Trans <transfire@gmail.com> wrote:
>
>
> On Jun 23, 6:09 pm, darren kirby <bulli...@badcomputer.org> wrote:
> > *Sigh*, I mean:
> >
> > # Writes embedded images to a file
> > #
> > # :call-seq:
> > # write_picture() -> nil
> > # write_picture(:outfile=>"str") -> nil
> > # write_picture(:n=>int) -> nil
> > # write_picture(:outfile=>"str", :n=>int) -> nil
> >
> > -d
>
> Bingo.
>
> But hey, as Resident Dreamer, I can't help but wonder how cool it
> might be to have highly descriptive and auto-flexible arguments:
>
> def write_picture(n <= Integer, outfile <= String)
> ...
> end
>
> write_picture("front cover", 1)
> write_picture(1, "front cover")
>
> Sacrifice a little duck-typiness for some arg-flippiness :)

Yuck.

Rick DeNatale

6/26/2007 7:23:00 PM

0

On 6/23/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:

> Oh, the new literal syntax is:
>
> { foo : 2, bar : 10 }
>
> so that'd be my_method( foo : 2, bar : 10 )
>
> But it's supposed to be an addition, not a replacement for the { :foo
> => 2 } syntax.


Yep, a little more syntactic sugar, or maybe saccharine, since it
slims down the expression a bit.<G>.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Trans

6/26/2007 8:37:00 PM

0



On Jun 23, 9:21 pm, "Gregory Brown" <gregory.t.br...@gmail.com> wrote:
> On 6/23/07, Trans <transf...@gmail.com> wrote:
>
>
>
>
>
> > On Jun 23, 6:09 pm, darren kirby <bulli...@badcomputer.org> wrote:
> > > *Sigh*, I mean:
>
> > > # Writes embedded images to a file
> > > #
> > > # :call-seq:
> > > # write_picture() -> nil
> > > # write_picture(:outfile=>"str") -> nil
> > > # write_picture(:n=>int) -> nil
> > > # write_picture(:outfile=>"str", :n=>int) -> nil
>
> > > -d
>
> > Bingo.
>
> > But hey, as Resident Dreamer, I can't help but wonder how cool it
> > might be to have highly descriptive and auto-flexible arguments:
>
> > def write_picture(n <= Integer, outfile <= String)
> > ...
> > end
>
> > write_picture("front cover", 1)
> > write_picture(1, "front cover")
>
> > Sacrifice a little duck-typiness for some arg-flippiness :)
>
> Yuck.

Is the "formalist" in you disturbed by this?

I think too many programmers feel that way too. See, I wonder when we
ever are going to get past conforming to the requirements of our
computer systems, and start having them conform to ours.

T.