PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Passing keyword arguments
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Passing keyword arguments

Réponse
 
LinkBack Outils de la discussion
Vieux 03/11/2007, 02h27   #1
transfire@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Passing keyword arguments

Lets say I have a method using the not-so-uncommon keyword options
pattern:

def foo(*args)
opts = (Hash === args.last ? args.pop : {})
...

Now I want to define another method, which is essentially a special
alias, that places a particular option as the first argument:

def bar(opt, *args)
foo(*args, :baz => opt) # doesn't work.
end

Is there no better way to do this than:

def bar(opt, *args)
opts = (Hash === args.last ? args.pop : {})
opts[:baz] = opt
args << opts
foo(*args)
end

Boy, it really makes you wish we had keyword arguments built-in to the
language!!! I forget, will we have those in 1.9? If so how the above
look then?

T.


  Réponse avec citation
Vieux 03/11/2007, 10h47   #2
robert.dober@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing keyword arguments

On Nov 3, 2007 1:27 AM, Trans <transfire@gmail.com> wrote:
> Lets say I have a method using the not-so-uncommon keyword options
> pattern:
>
> def foo(*args)
> opts = (Hash === args.last ? args.pop : {})


Hi Tom

irb(main):001:0> def foo *args
irb(main):002:1> opts, args = args.partition{|x| Hash === x }
irb(main):003:1> opts = opts.inject{ |s,x| s.update x }

seems this should do the trick
> ...
>
> Now I want to define another method, which is essentially a special
> alias, that places a particular option as the first argument:
>
> def bar(opt, *args)
> foo(*args, :baz => opt) # doesn't work.


now it does , almost
foo( :baz => opt, *args)

HTH
Robert
> end
>
> Is there no better way to do this than:
>
> def bar(opt, *args)
> opts = (Hash === args.last ? args.pop : {})
> opts[:baz] = opt
> args << opts
> foo(*args)
> end
>
> Boy, it really makes you wish we had keyword arguments built-in to the
> language!!! I forget, will we have those in 1.9? If so how the above
> look then?

Yes indeed I wished for them for a long time
R.
--
what do I think about Ruby?
http://ruby-smalltalk.blogspot.com/

  Réponse avec citation
Vieux 04/11/2007, 00h43   #3
transfire@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing keyword arguments



On Nov 3, 4:47 am, "Robert Dober" <robert.do...@gmail.com> wrote:
> On Nov 3, 2007 1:27 AM, Trans <transf...@gmail.com> wrote:
>
> > Lets say I have a method using the not-so-uncommon keyword options
> > pattern:

>
> > def foo(*args)
> > opts = (Hash === args.last ? args.pop : {})

>
> Hi Tom
>
> irb(main):001:0> def foo *args
> irb(main):002:1> opts, args = args.partition{|x| Hash === x }
> irb(main):003:1> opts = opts.inject{ |s,x| s.update x }
>
> seems this should do the trick
>
> > ...

>
> > Now I want to define another method, which is essentially a special
> > alias, that places a particular option as the first argument:

>
> > def bar(opt, *args)
> > foo(*args, :baz => opt) # doesn't work.

>
> now it does , almost
> foo( :baz => opt, *args)


Interesting approach. Thanks.

T.


  Réponse avec citation
Vieux 04/11/2007, 01h12   #4
bbxx789_05ss@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing keyword arguments

Robert Dober wrote:
> On Nov 3, 2007 1:27 AM, Trans <transfire@gmail.com> wrote:
>> Lets say I have a method using the not-so-uncommon keyword options
>> pattern:
>>
>> def foo(*args)
>> opts = (Hash === args.last ? args.pop : {})

>
> Hi Tom
>
> irb(main):001:0> def foo *args
> irb(main):002:1> opts, args = args.partition{|x| Hash === x }
> irb(main):003:1> opts = opts.inject{ |s,x| s.update x }
>
> seems this should do the trick
>> ...


Why not simply:

def foo(*args)
opts = args[0]
args = args[1..-1]

p opts, args
end

def bar(opt, *args)
foo(:baz =>opt, *args)
puts
foo(:baz =>opt, ther => 'pie', *args)
end

bar(10, 20, 30)


--output:--
{:baz=>10}
[20, 30]

{:baz=>10, ther=>"pie"}
[20, 30]

--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 04/11/2007, 01h47   #5
Brian Adkins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing keyword arguments

On Nov 2, 8:27 pm, transf...@gmail.com wrote:
> Lets say I have a method using the not-so-uncommon keyword options
> pattern:
>
> def foo(*args)
> opts = (Hash === args.last ? args.pop : {})
> ...
>
> Now I want to define another method, which is essentially a special
> alias, that places a particular option as the first argument:
>
> def bar(opt, *args)
> foo(*args, :baz => opt) # doesn't work.
> end
>
> Is there no better way to do this than:
>
> def bar(opt, *args)
> opts = (Hash === args.last ? args.pop : {})
> opts[:baz] = opt
> args << opts
> foo(*args)
> end
>
> Boy, it really makes you wish we had keyword arguments built-in to the
> language!!! I forget, will we have those in 1.9? If so how the above
> look then?


How flexible do you need to be with the argument handling? Would
something like the following work?

def foo a, b='default', opts={}
puts "a is #{a}, b is #{b}"
opts.each {|k,v| puts "#{k} => #{v}"}
puts '-'*10
end

def bar opt, a, b='default2', opts={}
foo a, b, opts.merge!({:baz => opt})
end

foo 1
foo 2, 'foo'
foo 3, 'foo', pt1 => 'one', pt2 => 'two'
bar 'extra', 4
bar 'extra', 5, 'bar', pt1 => 'one', pt2 => 'two'

Brian Adkins

  Réponse avec citation
Vieux 04/11/2007, 01h52   #6
robert.dober@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing keyword arguments

On Nov 4, 2007 1:12 AM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> Robert Dober wrote:
> > On Nov 3, 2007 1:27 AM, Trans <transfire@gmail.com> wrote:
> >> Lets say I have a method using the not-so-uncommon keyword options
> >> pattern:
> >>
> >> def foo(*args)
> >> opts = (Hash === args.last ? args.pop : {})

> >
> > Hi Tom
> >
> > irb(main):001:0> def foo *args
> > irb(main):002:1> opts, args = args.partition{|x| Hash === x }
> > irb(main):003:1> opts = opts.inject{ |s,x| s.update x }
> >
> > seems this should do the trick
> >> ...

>
> Why not simply:
>
> def foo(*args)
> opts = args[0]
> args = args[1..-1]
>
> p opts, args
> end
>
> def bar(opt, *args)
> foo(:baz =>opt, *args)
> puts
> foo(:baz =>opt, ther => 'pie', *args)
> end
>
> bar(10, 20, 30)
>
>
> --output:--
> {:baz=>10}
> [20, 30]
>
> {:baz=>10, ther=>"pie"}
> [20, 30]

because it does not work, try
bar( 1, 2, 3, :a=> 42)
And putting kw params before positionals is too unusual to be likely
to be used a lot
my solution is ugly because you have to exactly that but it is at
least hidden from the users of foo and bar.

R.


--
what do I think about Ruby?
http://ruby-smalltalk.blogspot.com/

  Réponse avec citation
Vieux 04/11/2007, 03h04   #7
transfire@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Passing keyword arguments



On Nov 3, 8:50 pm, Brian Adkins <lojicdot...@gmail.com> wrote:

> How flexible do you need to be with the argument handling? Would
> something like the following work?
>
> def foo a, b='default', opts={}
> puts "a is #{a}, b is #{b}"
> opts.each {|k,v| puts "#{k} => #{v}"}
> puts '-'*10
> end
>
> def bar opt, a, b='default2', opts={}
> foo a, b, opts.merge!({:baz => opt})
> end


Problem is I need *args (an untold amount).

T.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h25.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,35601 seconds with 15 queries