# Re: Hash#getopt

**URL:** <https://www.ruby-forum.com/t/re-hash-getopt/94216>\
**Category:** Ruby\
**Created:** [April 25, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/re-hash-getopt/94216 "2006-04-25T19:57:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 25, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/re-hash-getopt/94216/1 "2006-04-25T19:57:12Z")

</div>

> some\_method arg, “key” =\> “value”
> 
> or
> 
> some\_method arg, :key=\>:value
> 
> are idiomatic ruby. let’s just support it directly and be  
> done with it:

I’d rather have a class or instance level method that let me define this  
behavior globally for all hashes, or locally for instances. That way I  
can keep the standard aref syntax, e.g. arg[:key] instead of using a  
separate method name. I wouldn’t mind a way to ignore case, either.

h = {‘foo’, 1, ‘BAR’, 2}  
h.symbol\_alias = true # or whatever  
h.ignore\_key\_case = true # or whatever

h[‘foo’] # 1  
h[:foo] # 1  
h[“FOO”] # 1  
h[‘bar’] # 2  
… etc, etc.

That being said, I’ll take a Hash#getopt method over nothing. 🙂

Regards,

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 25, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/re-hash-getopt/94216/2 "2006-04-25T20:22:11Z")

</div>

On Wed, 26 Apr 2006, Berger, Daniel wrote:

> h[‘foo’] # 1  
> h[:foo] # 1  
> h[“FOO”] # 1  
> h[‘bar’] # 2  
> … etc, etc.
> 
> That being said, I’ll take a Hash#getopt method over nothing. 🙂

i agreed. the issue though, is that doing that sort of thing globally  
for  
hashes requires a good bit of code to make something like this work

opts.values\_at :key, ‘key’

or

opts\_a = {:key =\> 42}  
opts\_b = {‘key’ =\> 42.0}

opts\_a.update opts\_b

by sticking to access of options only it’s possible to provide an easy  
way to  
do the normal thing in 98% of cases. as the above two examples show  
though, a  
more general approach involves making lots of choices about  
string/symbol  
interaction. i actually do this in my own code but it’s slippery and  
not for  
general consumption.

regards.

-a
