Shortcut for "if options.key?(:full)" ?

P

Peter Alvin

If I pass an options hash into a def, how do I easily check to see if an
option is set? This is how I'm doing it now:

if options.key?:)some_option)
...
end

Knowing Ruby, there's got to be a cleaner/better way!

Pete
 
R

Robert Klemme

2008/9/23 Peter Alvin said:
If I pass an options hash into a def, how do I easily check to see if an
option is set? This is how I'm doing it now:

if options.key?:)some_option)
...
end

Knowing Ruby, there's got to be a cleaner/better way!

If nil is not used as valid option value you can usually do

if options[:some_option]

I don't see how it can become much shorter - unless you rename
"options" and options.

Cheers

robert
 
P

Phlip

if options.key?:)some_option)
...
end

Knowing Ruby, there's got to be a cleaner/better way!

You can generally use options.fetch:)some_option, default) to pass a default
thru without any 'if' statements...
 

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

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,648
Latest member
NREEugene

Latest Threads

Top