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 > blocks expected arguments and metaprogramming
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
blocks expected arguments and metaprogramming

Réponse
 
LinkBack Outils de la discussion
Vieux 16/09/2007, 16h10   #1 (permalink)
Pedro Del Gallego
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut blocks expected arguments and metaprogramming

hi all,

in this (http://pastie.caboo.se/97662 ) code :

module Kernel
def expected_argument_list
2
end

def before (name)
puts :hola
define_method(name){
if expected_argument_list==1
yield nly_one
else
yield ne,:two
end
}
end
end

class A
before :foo do |*args|
puts second
end

before :foo do |first , second|
puts second
end
end

a = A.new
a.foo

Does anyone know of a way to determine what argument list was
specified when the block is created?

i mean i want to implement the function expected_argument_list, to
return the length of the argument list that the block expected.
Probably is not possible to do that

When are defined the variables of the block, in the class definition
time, or after the yield call?

Thanks

--
-------------------------------------
Pedro Del Gallego

Email : pedro.delgallego@gmail.com

  Réponse avec citation
Vieux 16/09/2007, 17h56   #2 (permalink)
Wilson Bilkovich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: blocks expected arguments and metaprogramming

On 9/16/07, Pedro Del Gallego <pedro.delgallego@gmail.com> wrote:
> hi all,
>
> in this (http://pastie.caboo.se/97662 ) code :
>
> module Kernel
> def expected_argument_list
> 2
> end
>
> def before (name)
> puts :hola
> define_method(name){
> if expected_argument_list==1
> yield nly_one
> else
> yield ne,:two
> end
> }
> end
> end
>
> class A
> before :foo do |*args|
> puts second
> end
>
> before :foo do |first , second|
> puts second
> end
> end
>
> a = A.new
> a.foo
>
> Does anyone know of a way to determine what argument list was
> specified when the block is created?
>
> i mean i want to implement the function expected_argument_list, to
> return the length of the argument list that the block expected.
> Probably is not possible to do that
>
> When are defined the variables of the block, in the class definition
> time, or after the yield call?
>


The block arguments are only known at runtime, since you could do this:
def example
args = []
rand(15).times {|i| args << i}
yield *args
end

  Réponse avec citation
Vieux 16/09/2007, 18h00   #3 (permalink)
Trans
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: blocks expected arguments and metaprogramming



On Sep 16, 8:10 am, "Pedro Del Gallego" <pedro.delgall...@gmail.com>
wrote:
> hi all,
>
> in this (http://pastie.caboo.se/97662) code :
>
> module Kernel
> def expected_argument_list
> 2
> end
>

def before (name, &blk)
puts :hola
define_method(name){
if blk.arity==1
yield nly_one
else
yield ne,:two
end
}
end
end

It's not quite as simple as that but that should be enough to move you
forward.

T.


  Réponse avec citation
Vieux 16/09/2007, 18h40   #4 (permalink)
Pedro Del Gallego
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: blocks expected arguments and metaprogramming

>
> On Sep 16, 8:10 am, "Pedro Del Gallego" <pedro.delgall...@gmail.com>
> wrote:
> > hi all,
> >
> > in this (http://pastie.caboo.se/97662) code :
> >
> > module Kernel
> > def expected_argument_list
> > 2
> > end
> >

> def before (name, &blk)
> puts :hola
> define_method(name){
> if blk.arity==1
> yield nly_one
> else
> yield ne,:two
> end
> }
> end
> end
>
> It's not quite as simple as that but that should be enough to move you
> forward.


Yes thats enough for me. I didnt now that the block was a Proc class.

Thanks.


--
-------------------------------------
Pedro Del Gallego

Email : pedro.delgallego@gmail.com

  Réponse avec citation
Vieux 16/09/2007, 19h06   #5 (permalink)
Pedro Del Gallego
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: blocks expected arguments and metaprogramming

BTW, its works perfectly, with that i can decide what parameter return
in advances, (but, yet, I dont know is a good idea or not)

Thanks

module Kernel
def before (name, &block)
define_method(name) do
if block.arity == 1
yield nly_one
elsif block.arity == 2
yield ne,:two
elsif block.arity == -2
yield ne, :a , :splater, :argument
end
end
end
end

class A
before :foo do |first|
puts first
end
end

a = A.new
a.foo

class A
before :foo do |first , second|
puts second
end
end

a2 = A.new
a2.foo

class A
before :foo do |first , *splat_arg|
puts splat_arg
end
end

a3 = A.new
a3.foo


2007/9/16, Pedro Del Gallego <pedro.delgallego@gmail.com>:
> >
> > On Sep 16, 8:10 am, "Pedro Del Gallego" <pedro.delgall...@gmail.com>
> > wrote:
> > > hi all,
> > >
> > > in this (http://pastie.caboo.se/97662) code :
> > >
> > > module Kernel
> > > def expected_argument_list
> > > 2
> > > end
> > >

> > def before (name, &blk)
> > puts :hola
> > define_method(name){
> > if blk.arity==1
> > yield nly_one
> > else
> > yield ne,:two
> > end
> > }
> > end
> > end
> >
> > It's not quite as simple as that but that should be enough to move you
> > forward.

>
> Yes thats enough for me. I didnt now that the block was a Proc class.
>
> Thanks.
>
>
> --
> -------------------------------------
> Pedro Del Gallego
>
> Email : pedro.delgallego@gmail.com
>
>



--
-------------------------------------
Pedro Del Gallego

Email : pedro.delgallego@gmail.com

  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 03h05.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12176 seconds with 13 queries