Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > hash-style parameters

Reply
Thread Tools

hash-style parameters

 
 
Terry Michaels
Guest
Posts: n/a
 
      08-14-2010
Some function calls I've seen, like in Shoes, look like so:

para "Paragraph Text", :style => "this", :whatever => "that"

How do I define a function call so that can take one "normal" parameter
and then an unknown number of key/value pairs?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
David Masover
Guest
Posts: n/a
 
      08-14-2010
On Saturday, August 14, 2010 01:23:21 pm Terry Michaels wrote:
> Some function calls I've seen, like in Shoes, look like so:
>
> para "Paragraph Text", :style => "this", :whatever => "that"
>
> How do I define a function call so that can take one "normal" parameter
> and then an unknown number of key/value pairs?


You could answer your own question by looking at the source of the program
doing this -- and I'd suggest it for anything you want to learn more about.

For this case, remember that this basically becomes:

para("Paragraph Text", {:style => "this", :whatever => "that"})

And of course, there's no reason those literals need to be there -- you could
just do this:

text = "Paragraph Text"
hash = {:style => "this", :whatever => "that"}
para(text, hash)

So, it should be clear that this is what you want:

def para text, options

There is one catch, though -- you said "unknown number", which could be zero,
so you need a default value:

def para text, options={}

 
Reply With Quote
 
 
 
 
Pablo Torres N.
Guest
Posts: n/a
 
      08-14-2010
On Sat, Aug 14, 2010 at 13:58, David Masover <> wrote:
> So, it should be clear that this is what you want:
>
> def para text, options
>
> There is one catch, though -- you said "unknown number", which could be zero,
> so you need a default value:
>
> def para text, options={}


Just a note: while you can omit parens in method definitions, most
code does only if the method takes no arguments, if it does, leave the
parens there.

 
Reply With Quote
 
David Masover
Guest
Posts: n/a
 
      08-15-2010
On Saturday, August 14, 2010 05:50:22 pm Pablo Torres N. wrote:
> On Sat, Aug 14, 2010 at 13:58, David Masover <> wrote:
> > So, it should be clear that this is what you want:
> >
> > def para text, options
> >
> > There is one catch, though -- you said "unknown number", which could be
> > zero, so you need a default value:
> >
> > def para text, options={}

>
> Just a note: while you can omit parens in method definitions, most
> code does only if the method takes no arguments, if it does, leave the
> parens there.


Why?

I find it more readable without parens, but with syntax highlighting, mostly
because that's how I call methods.

 
Reply With Quote
 
Pablo Torres N.
Guest
Posts: n/a
 
      08-15-2010
On Sun, Aug 15, 2010 at 00:56, David Masover <> wrote:
> On Saturday, August 14, 2010 05:50:22 pm Pablo Torres N. wrote:
>> Just a note: while you can omit parens in method definitions, most
>> code does only if the method takes no arguments, if it does, leave the
>> parens there.

>
> Why?
>
> I find it more readable without parens, but with syntax highlighting, mostly
> because that's how I call methods.
>
>


Just a convention, I guess precisely so you can easily tell method
definitions and method invocations apart. Consider:

def say_hi(tittle, name)
puts "Hi, #{tittle} #{name}"
end

tittle = "Dr."
name = "Cham"

some_obj.instance_eval do
def say_hi tittle, name
puts "Howdy, #{tittle} #{name}"
end
end

A bit ambiguous.

 
Reply With Quote
 
Roger Pack
Guest
Posts: n/a
 
      08-16-2010

> def para text, options
>
> There is one catch, though -- you said "unknown number", which could be
> zero,
> so you need a default value:
>
> def para text, options={}


There is also the "named parameter" gem which might be useful to you:
http://github.com/rdp/arguments
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
commands to configure WLAN adapter parameters =?Utf-8?B?SmFnYW4=?= Wireless Networking 0 10-05-2005 11:37 AM
does a "parameters"-parameter overwrite the "parameters"-object? Florian Loitsch Javascript 11 03-15-2005 03:33 PM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57