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 > Create method using define_method with all sort of args?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Create method using define_method with all sort of args?

Réponse
 
LinkBack Outils de la discussion
Vieux 22/11/2007, 09h59   #1
Joshua Muheim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Create method using define_method with all sort of args?

Hi all

I know that one can create new methods using define_method...

def new_method
end

is equivalent to

define_method(:new_method) do
end

def new_method(some_param)
end

is equivalent to

define_method(:new_method) do |some_param|
end

def new_method(some_param, *args)
end

is equivalent to

define_method(:new_method) do |some_param, *args|
end

But what's equivalent to...

def new_method(some_param, options = {}, *args)
end

??? Thanks for .
Josh
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/11/2007, 12h52   #2
Joshua Muheim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Create method using define_method with all sort of args?

Oh yeah, forgot one...

def new_method(some_param, options = {}, *args, &block)
end

What's the equivalent to this...?
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 23/11/2007, 07h55   #3
MonkeeSage
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Create method using define_method with all sort of args?

On Nov 22, 6:52 am, Joshua Muheim <fo...@josh.ch> wrote:

> What's the equivalent to this...?


You have to use a predefined function if you want to pass a block,
e.g,

class A
def new_method1(somep, opts={}, *args, &block)
p opts
end
private
def _foo(somep, opts={}, *args, &block)
p opts
end
public
define_method(:new_method2, instance_method(:_foo))
end

Also, you can't pass an optional argument to a block, you'd have to
write something like...

class A
define_method(:new_method3) { |somep, *args|
opts = {}
if args.length > 0 && args[0].class == Hash
opts = args[0]
end
p opts
}
end

Regards,
Jordan
  Réponse avec citation
Vieux 23/11/2007, 20h23   #4
ara.t.howard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Create method using define_method with all sort of args?


On Nov 22, 2007, at 2:59 AM, Joshua Muheim wrote:

> But what's equivalent to...
>
> def new_method(some_param, options = {}, *args)
> end


define_method 'new_method' do |some_param, *argv|
options, *argv = argv
options ||= {}

...
end

a @ http://codeforpeople.com/
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama



  Réponse avec citation
Vieux 24/11/2007, 02h26   #5
MonkeeSage
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Create method using define_method with all sort of args?

On Nov 23, 2:23 pm, "ara.t.howard" <ara.t.how...@gmail.com> wrote:

> define_method 'new_method' do |some_param, *argv|
> options, *argv = argv
> options ||= {}
>
> ...
> end


Ara,

Isn't that dangerous in case new_method is called like:

a.new_method("a", other, args)

....where the optional 'option' arg is left out? It seems that your
version would consume the 'other' operand in that case. I think you'd
need to do something like I posted above to be safe.

Regards,
Joran
  Réponse avec citation
Vieux 24/11/2007, 04h16   #6
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Create method using define_method with all sort of args?

On Nov 23, 7:26 pm, MonkeeSage <MonkeeS...@gmail.com> wrote:
> On Nov 23, 2:23 pm, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
>
> > define_method 'new_method' do |some_param, *argv|
> > options, *argv = argv
> > options ||= {}

>
> > ...
> > end

>
> Ara,
>
> Isn't that dangerous in case new_method is called like:
>
> a.new_method("a", other, args)
>
> ...where the optional 'option' arg is left out? It seems that your
> version would consume the 'other' operand in that case. I think you'd
> need to do something like I posted above to be safe.


Your question implies to me that you think that if I define a method
like this:
def foo( name, age=0, weight=170 )
...
end
that I can then call it like this:
foo( "Gavin", 175 ) # use default age.

In case I'm properly understanding you, then you should know that you
cannot do that. You can't 'skip' an optional argument and have other
optional or non-optional arguments later. So, by definition, the
'other' you use in your example is necessarily the second parameter to
the method, and thus satisfies the OPs desired functionality.
  Réponse avec citation
Vieux 24/11/2007, 04h30   #7
MonkeeSage
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Create method using define_method with all sort of args?

On Nov 23, 10:16 pm, Phrogz <phr...@mac.com> wrote:

> In case I'm properly understanding you, then you should know that you
> cannot do that.


Err...my brain broke for a minute there (bad brain!), heh. Thanks for
the correction.

Regards,
Jordan
  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 06h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, 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,14916 seconds with 15 queries