method interface - hash argument vs. named parameters

I

Intransition

I've never run into this issue before, but I'm working on a method
interface that is quite 'dense', and I need to be able to
differentiate between a hash passed to a method and named parameters.
i.e. All of these are valid:

foo(hash, :eek:pt=>val)
foo(hash)
foo:)opt=>val)

However, there seems to be no way to distinguish that last the two
forms.

Is there any way? Or am I stuck?
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

I've never run into this issue before, but I'm working on a method
interface that is quite 'dense', and I need to be able to
differentiate between a hash passed to a method and named parameters.
i.e. All of these are valid:

foo(hash, :eek:pt=>val)
foo(hash)
foo:)opt=>val)

However, there seems to be no way to distinguish that last the two
forms.

Is there any way? Or am I stuck?
I would expect stuck, because the difference seems nothing more than visual.
(ie if the options hash was assigned to a var, rather than created within
the arg list, then the last two would be exactly the same) though it does
seem unusual that there is not a method like options_given? the way there is
a block_given?

I suppose if this method interface is absolutely necessary, then a hack you
might be able to get away with would be to check the keys to see if they
match up with your options. But I would be embarrassed to show anyone such a
solution ;)

Something to consider, though, whatever param the hash is an arg for, is
optional (ie omitted in your third invocation). So perhaps it would fit
better into the options hash, which you could then extract it from, inside
the method:

def foo( options = Hash.new )
the_hash = options.delete:)the_hash) { Hash.new }
[ the_hash , options ]
end

hash = Hash[*1..4]
val = :val

foo( :the_hash => hash , :eek:pt => val ) # => [{1=>2, 3=>4}, {:eek:pt=>:val}]
foo( :the_hash => hash ) # => [{1=>2, 3=>4}, {}]
foo( :eek:pt => val ) # => [{}, {:eek:pt=>:val}]
 
R

Roger Pack

foo(hash)
foo:)opt=>val)

However, there seems to be no way to distinguish that last the two
forms.

One possibility would be to check for known keys in the hash. If
they're there, assume it's an options hash.
Another might be to allow them to input it like
:hash => hash
if they ever want it that way.
But you're probably just stuck.
-r
ref: http://github.com/rdp/arguments
 
I

Intransition

Is there any way? Or am I stuck?

I would expect stuck, because the difference seems nothing more than visu= al.
(ie if the options hash was assigned to a var, rather than created within
the arg list, then the last two would be exactly the same) though it does
seem unusual that there is not a method like options_given? the way there= is
a block_given?

I suppose if this method interface is absolutely necessary, then a hack y= ou
might be able to get away with would be to check the keys to see if they
match up with your options. But I would be embarrassed to show anyone suc= h a
solution ;)

Something to consider, though, whatever param the hash is an arg for, is
optional (ie omitted in your third invocation). So perhaps it would fit
better into the options hash, which you could then extract it from, insid= e
the method:

def foo( options =3D Hash.new )
=A0 the_hash =3D options.delete:)the_hash) { Hash.new }
=A0 [ the_hash , options ]
end

hash =3D Hash[*1..4]
val =3D :val

foo( :the_hash =3D> hash , :eek:pt =3D> val ) =A0# =3D> [{1=3D>2, 3=3D>4}, {= :eek:pt=3D>:val}]
foo( :the_hash =3D> hash ) =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# =3D> [{1=3D>2= , 3=3D>4}, {}]
foo( :eek:pt =3D> val ) =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# =3D> [{=
}, {:eek:pt=3D>:val}]

Yep. That's what I thought. Appreciate the suggestion. In my case I've
decided I'll just have to do without the options hash.

I was hoping Ruby 1.9 at least would have had this licked. Early on
there was talk of named parameters as:

def foo(hash=3Dnil, **opts)

But it never came to pass.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top