[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

best way to assign args ???

pere.noel

8/3/2006 10:34:00 AM

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 ???
--
une bévue
2 Answers

Morton Goldberg

8/3/2006 1:18:00 PM

0

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

On Aug 3, 2006, at 6:35 AM, Une bévue wrote:

> 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 ???

pere.noel

8/3/2006 1:25:00 PM

0

Morton Goldberg <m_goldberg@ameritech.net> 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
--
une bévue