# Best way to assign args?

**URL:** <https://www.ruby-forum.com/t/best-way-to-assign-args/101188>\
**Category:** Ruby\
**Created:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/best-way-to-assign-args/101188 "2006-08-03T14:23:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/best-way-to-assign-args/101188/1 "2006-08-03T14:23:43Z")

</div>

on Mac OS X using launchd i’m only able to pass args like that :

[“DEBUGG=true”, “auto=true”, “dummy=empty”,  
“files=/Users/yvon/MacSOUP\_proxad/proxad,  
/Users/yvon/MacSOUP\_eclipse/eclipse,  
/Users/yvon/MacSOUP\_news.individual.net/individual”]

then for the time being i do :

@h={}  
$\*.each {|a| k,v=a.split("="); @h[k]=v}

and, afterwards :

@h[‘DEBUGG’]=(@h[‘DEBUGG’]==“true”)  
for bool, and for array :

@h[‘files’]=@h[‘files’].split(", ")

i don’t like that because i have to know, a priori, the types and names  
of the args.

a better solution more rubyish ???

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/best-way-to-assign-args/101188/2 "2006-08-03T15:18:57Z")

</div>

The following does what you want in one pass:

INP = [“DEBUGG=true”, “auto=true”, “dummy=empty”,  
“files=/Users/yvon/MacSOUP\_proxad/proxad, /Users/yvon/MacSOUP\_eclipse/  
eclipse, /Users/yvon/MacSOUP\_news.individual.net/individual”]

@h = {}  
INP.each do |a|  
k,v = a.split("=")  
case v  
when /true/ then @h[k] = true  
when /false/ then @h[k] = false  
when /,[\s]_/ then @h[k] = v.split(/,[\s]_/)  
else @h[k] = v  
end  
end

It is easily extend to other cases if necessary. Whether or not it is  
better or more Ruby-ish is for you to decide.

Hope this helps.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 3:27pm UTC](https://www.ruby-forum.com/t/best-way-to-assign-args/101188/3 "2006-08-03T15:27:58Z")

</div>

Morton G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The following does what you want in one pass:

fanstatic !

> [snip]
> 
> It is easily extend to other cases if necessary. Whether or not it is  
> better or more Ruby-ish is for you to decide.
> 
> Hope this helps.

yes it helps me having different solutions, at least.

thanx

Yvon
