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 > proc to block
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
proc to block

Réponse
 
LinkBack Outils de la discussion
Vieux 08/01/2008, 04h06   #1
L A
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut proc to block

[Note: parts of this message were removed to make it a legal post.]

Hello all,

Is there a way to turn a Proc into a block for the purposes of methods that yield to blocks? It's not a necessity as I can always wrap the Proc in a block, I'm just curious.

Thanks,
Loren




__________________________________________________ __________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
  Réponse avec citation
Vieux 08/01/2008, 04h35   #2
Gary Wright
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc to block


On Jan 7, 2008, at 11:06 PM, L A wrote:

> Hello all,
>
> Is there a way to turn a Proc into a block for the purposes of
> methods that yield to blocks? It's not a necessity as I can always
> wrap the Proc in a block, I'm just curious.


Just prefix the object you want to be passed as a block with '&' in
the argument list.

p = proc { |x| x > 10 }

[1,5,10,20, 30].select &p


Gary Wright

  Réponse avec citation
Vieux 08/01/2008, 04h36   #3
botp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc to block

On Jan 8, 2008 12:06 PM, L A <loren.abrams@yahoo.com> wrote:
> Is there a way to turn a Proc into a block for the purposes of methods that yield to blocks? It's not a necessity as I can always wrap the Proc in a block, I'm just curious.


maybe use the & op, like

p=proc{"hi, i'm proc"}
#=> #<Proc:0x028cc40c@(irb):1>
p.call
#=> "hi, i'm proc"
def q
yield
end
#=> nil
q
LocalJumpError: no block given
from (irb):7:in `q'
from (irb):9
from :0
q &p
#=> "hi, i'm proc"
(proc &p).call
#=> "hi, i'm proc"

i am not sure why you want to wrap a proc with a block though

proc {p}
#=> #<Proc:0x02898418@(irb):30>
(proc {p}).call
#=> #<Proc:0x028cc40c@(irb):1>
(proc {p}).call.call
#=> "hi, i'm proc"

kind regards -botp

  Réponse avec citation
Vieux 08/01/2008, 13h24   #4
Rick DeNatale
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc to block

On Jan 7, 2008 11:35 PM, Gary Wright <gwtmp01@mac.com> wrote:
>
> On Jan 7, 2008, at 11:06 PM, L A wrote:
>
> > Hello all,
> >
> > Is there a way to turn a Proc into a block for the purposes of
> > methods that yield to blocks? It's not a necessity as I can always
> > wrap the Proc in a block, I'm just curious.

>
> Just prefix the object you want to be passed as a block with '&' in
> the argument list.
>
> p = proc { |x| x > 10 }
>
> [1,5,10,20, 30].select &p


Except that this isn't part of the standard ruby library for 1.8. It
relies on having a Symbol#to_proc method

It is a common extension, for example Rails includes it.

It IS part of Ruby 1.9 though.

I'd suggest googling for "ruby to_proc"


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

  Réponse avec citation
Vieux 08/01/2008, 14h04   #5
Noah Easterly
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc to block

On Jan 8, 8:24 am, Rick DeNatale <rick.denat...@gmail.com> wrote:
> On Jan 7, 2008 11:35 PM, Gary Wright <gwtm...@mac.com> wrote:
>
>
>
> > On Jan 7, 2008, at 11:06 PM, L A wrote:

>
> > > Hello all,

>
> > > Is there a way to turn a Proc into a block for the purposes of
> > > methods that yield to blocks? It's not a necessity as I can always
> > > wrap the Proc in a block, I'm just curious.

>
> > Just prefix the object you want to be passed as a block with '&' in
> > the argument list.

>
> > p = proc { |x| x > 10 }

>
> > [1,5,10,20, 30].select &p

>
> Except that this isn't part of the standard ruby library for 1.8. It
> relies on having a Symbol#to_proc method
>

Wait... what?

No it doesn't.

[ 1, 5, 10, 20, 30 ].map &:to_a

uses Symbol#to_proc

p = proc { |x| x > 10 }
[1,5,10,20, 30].select &p

is standard ruby syntax, even from 1.6:

"If the last argument to a method is preceded by an ampersand, Ruby
assumes that it is a Proc object. It removes it from the parameter
list, converts the Proc object into a block, and associates it with
the method."
(see http://www.ruby-doc.org/docs/Program...ethods.html#UD)
  Réponse avec citation
Vieux 08/01/2008, 15h39   #6
Rick DeNatale
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc to block

On Jan 8, 2008 9:05 AM, Noah Easterly <noah.easterly@gmail.com> wrote:
> On Jan 8, 8:24 am, Rick DeNatale <rick.denat...@gmail.com> wrote:
> > On Jan 7, 2008 11:35 PM, Gary Wright <gwtm...@mac.com> wrote:
> > > Just prefix the object you want to be passed as a block with '&' in
> > > the argument list.

> >
> > > p = proc { |x| x > 10 }

> >
> > > [1,5,10,20, 30].select &p

> >
> > Except that this isn't part of the standard ruby library for 1.8. It
> > relies on having a Symbol#to_proc method
> >

> Wait... what?
>
> No it doesn't.
>
> [ 1, 5, 10, 20, 30 ].map &:to_a
>
> uses Symbol#to_proc
>
> p = proc { |x| x > 10 }
> [1,5,10,20, 30].select &p
>
> is standard ruby syntax, even from 1.6:
>
> "If the last argument to a method is preceded by an ampersand, Ruby
> assumes that it is a Proc object. It removes it from the parameter
> list, converts the Proc object into a block, and associates it with
> the method."


This isn't what I said.

Gary suggested using a symbol as a proc, which requires that Symbol
implement the to_proc method, which is NOT part of standard Ruby prior
to 1.9.

$ qri to_proc
------------------------------------------------------ Multiple choices:

Method#to_proc, Proc#to_proc, Test::Unit::Util::ProcWrapper#to_proc

Now as I pointed out extending Symbol to implement to_proc is quite
common, and many Rails programmers tend to think of the stuff in
ActiveSupport as being standard Ruby, but it isn't.

It's likely that Rails will change ActiveSupport to only add
Symbol#to_proc if using Ruby < 1.9 since 1.9 includes it, but that
doesn't seem to have happened yet.
http://dev.rubyonrails.org/ticket/8818

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

  Réponse avec citation
Vieux 08/01/2008, 15h55   #7
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc to block

Rick DeNatale wrote:
> Gary suggested using a symbol as a proc


No, he didn't.


--
Jabber: sepp2k@jabber.org
ICQ: 205544826

  Réponse avec citation
Vieux 08/01/2008, 16h32   #8
Gary Wright
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc to block


On Jan 8, 2008, at 10:39 AM, Rick DeNatale wrote:
> Gary suggested using a symbol as a proc, which requires that Symbol
> implement the to_proc method, which is NOT part of standard Ruby prior
> to 1.9.


Rick, you just misread my original post a bit. I didn't mention
Symbol#to_proc at all but I did hint at it by saying you 'just'
prefix the argument with '&'. That sort of begs the question of
what happens if the argument isn't an instance of Proc but I didn't
want to muddy the waters at that point. My example used a proc.

Gary Wright

  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 12h39.


É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,16202 seconds with 16 queries