PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Passing only a hash key, with no value, as an option
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Passing only a hash key, with no value, as an option

Réponse
 
LinkBack Outils de la discussion
Vieux 12/03/2008, 13h08   #1
Max Williams
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Passing only a hash key, with no value, as an option

I'm using the method of parameter passing where i have one 'regular'
parameter and then a hash of options:

def chunkify(something, options = {})
...
end

I want to have it so that the user can just pass a key through without
having to set it to anything, eg

dinner = chunkify(bacon, :extra_fine)

However, 'extra_fine' is just being treated as a symbol, rather than a
hash key, and so 'options' is simply the symbol :extra_fine, rather than
the hash {:extra_fine => nil}.

I can fix this by passing :extra_fine => true to the method, but can i
set up the method so it will just take the hash key, and maybe some
other hash_keys, some with and some without values?

thanks
max
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 12/03/2008, 13h18   #2
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing only a hash key, with no value, as an option

2008/3/12, Max Williams <toastkid.williams@gmail.com>:
> I'm using the method of parameter passing where i have one 'regular'
> parameter and then a hash of options:
>
> def chunkify(something, options = {})
> ...
> end
>
> I want to have it so that the user can just pass a key through without
> having to set it to anything, eg
>
> dinner = chunkify(bacon, :extra_fine)
>
> However, 'extra_fine' is just being treated as a symbol, rather than a
> hash key, and so 'options' is simply the symbol :extra_fine, rather than
> the hash {:extra_fine => nil}.
>
> I can fix this by passing :extra_fine => true to the method, but can i
> set up the method so it will just take the hash key, and maybe some
> other hash_keys, some with and some without values?


Nope. You will have to use chunkify(something, *options) and do the
processing yourself. Note that in no case can you mix Hash entries
with and without values. But you can do

irb(main):006:0> def x(*a)p a end
=> nil
irb(main):007:0> x(:foo, :bar => 2)
[:foo, {:bar=>2}]
=> nil
irb(main):008:0>

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

  Réponse avec citation
Vieux 12/03/2008, 13h19   #3
David A. Black
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing only a hash key, with no value, as an option

Hi --

On Wed, 12 Mar 2008, Max Williams wrote:

> I'm using the method of parameter passing where i have one 'regular'
> parameter and then a hash of options:
>
> def chunkify(something, options = {})
> ...
> end
>
> I want to have it so that the user can just pass a key through without
> having to set it to anything, eg
>
> dinner = chunkify(bacon, :extra_fine)
>
> However, 'extra_fine' is just being treated as a symbol, rather than a
> hash key, and so 'options' is simply the symbol :extra_fine, rather than
> the hash {:extra_fine => nil}.
>
> I can fix this by passing :extra_fine => true to the method, but can i
> set up the method so it will just take the hash key, and maybe some
> other hash_keys, some with and some without values?


You're over-thinking this a bit. :extra_fine isn't any more a hash key
than 100 is in this:

do_something(100)

The only thing that tells Ruby that you want a hash as the last
argument, in the absence of curly braces, is the hash separator (=>).

do_something(100 => "C")

There's also no such thing as a hash key that isn't part of a hash. It
sounds like what you want to do is pass multiple objects to your
method and create a hash from them inside the method.


David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!

  Réponse avec citation
Vieux 12/03/2008, 13h35   #4
Max Williams
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing only a hash key, with no value, as an option


>
> There's also no such thing as a hash key that isn't part of a hash. It
> sounds like what you want to do is pass multiple objects to your
> method and create a hash from them inside the method.
>
>
> David


Hi David

I know what you're saying, it is a bit clumsy.

Where i'm coming from is that i often use a lot of options that are set
to true by the user of the method if they want to use them, which does
work nicely due to options[:foo] evaluating to nil if the user didn't
set it, allowing me to have tests like
if options[:foo] #missing key equivalent to :foo => false
do_extra_stuff
end

I just thought it would be even nicer to have the user just be able to
pass the hash key, rather than have to keep adding "=> true". So the
presence of a hash key acts like a switch which i can test with

if options.has_key? :foo
do_extra_stuff
end

I thought i'd seen this functionality in rails, where we can pass just a
hash key without bothering to set it, if we want to switch something on
or off.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 12/03/2008, 13h42   #5
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing only a hash key, with no value, as an option

2008/3/12, Max Williams <toastkid.williams@gmail.com>:
>
> >
> > There's also no such thing as a hash key that isn't part of a hash. It
> > sounds like what you want to do is pass multiple objects to your
> > method and create a hash from them inside the method.
> >
> >
> > David

>
> Hi David
>
> I know what you're saying, it is a bit clumsy.


Pardon me, but it seems David's words did not sink in since you are
still talking about a Hash key where you mean "symbol".

> Where i'm coming from is that i often use a lot of options that are set
> to true by the user of the method if they want to use them, which does
> work nicely due to options[:foo] evaluating to nil if the user didn't
> set it, allowing me to have tests like
> if options[:foo] #missing key equivalent to :foo => false
> do_extra_stuff
> end
>
> I just thought it would be even nicer to have the user just be able to
> pass the hash key, rather than have to keep adding "=> true". So the
> presence of a hash key acts like a switch which i can test with
>
> if options.has_key? :foo
> do_extra_stuff
> end
>
> I thought i'd seen this functionality in rails, where we can pass just a
> hash key without bothering to set it, if we want to switch something on
> or off.


If you need a set of boolean flags then you can just use an array

irb(main):008:0> def x(*opts)
irb(main):009:1> settings = opts.inject({}){|h,o| h[o]=true;h}
irb(main):010:1> end
=> nil
irb(main):011:0> x(:foo, :lazy)
=> {:lazy=>true, :foo=>true}
irb(main):012:0>

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

  Réponse avec citation
Vieux 12/03/2008, 14h25   #6
Paul Mckibbin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing only a hash key, with no value, as an option

Max Williams wrote:
> I'm using the method of parameter passing where i have one 'regular'
> parameter and then a hash of options:
>
> def chunkify(something, options = {})
> ...
> end
>
> I want to have it so that the user can just pass a key through without
> having to set it to anything, eg
>
> dinner = chunkify(bacon, :extra_fine)
>
> However, 'extra_fine' is just being treated as a symbol, rather than a
> hash key, and so 'options' is simply the symbol :extra_fine, rather than
> the hash {:extra_fine => nil}.
>

extra_fine is a symbol, as other people pointed out.

If that's the only concern, then how about

def chunkify(something, options={})
options={options=>true} unless options.kind_of(Hash)
....

end

options=:extra_fine ;=> :extra_fine
options={options=>true} unless options.kind_of?(Hash)
;=>{:extra_fine=>true}

Then your normal processing can continue.

--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 12/03/2008, 14h30   #7
Max Williams
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing only a hash key, with no value, as an option


> options=:extra_fine ;=> :extra_fine
> options={options=>true} unless options.kind_of?(Hash)
> ;=>{:extra_fine=>true}
>
> Then your normal processing can continue.


OK, point taken

That's a nice snippet, thanks.

cheers all
max
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h05.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14789 seconds with 15 queries