[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

passing procs around in variables

Nathan

3/9/2008 10:49:00 PM

Hi,
This might be something really simple, but I'm trying to write a
console-based menu for my program. I've done this before in a
procedural language, but I decided to try and port what I've done to
ruby, because it'll make other parts of my program far easier. My
program has a few menus, so it'd be nice to have re-usable code (which
the procedural version didn't have much of).

The best way I've come up with is to pass the method calls which are
executed by each menu item as procs, but this requires passing them
around between two objects internally. (I haven't completely decided
how I'm going to structure the code which passes the procs... I have
an idea, but I'll cross that when I come to it)

Now, the Item class, which each menu item is an object of, doesn't
work as expected... take a look here: http://pastie.caboo....
When I try to call myItem.execute!, it complains "no block given". If
I call myItem.execute! { 2 + 1}, then it works, but that's not what I
want...

I've also included the Menu class, just in case anybody has any advice
as to how to do the whole thing better.

Thanks,
-Nathan

4 Answers

Stefan Lang

3/9/2008 11:02:00 PM

0

2008/3/9, Nathan <njmacinnes@gmail.com>:
> [...]
> Now, the Item class, which each menu item is an object of, doesn't
> work as expected... take a look here: http://pastie.caboo....
> When I try to call myItem.execute!, it complains "no block given". If
> I call myItem.execute! { 2 + 1}, then it works, but that's not what I
> want...

> class Item
> attr_reader :text
>
> def initialize(description, &proc)
> @description = description
> @proc = proc
> end
>
> def execute!
> yield @proc
> end
> end

Use yield when the block is _not_ stored in a variable.
What your yield line does is this: Call the block given to
execute! with @proc as argument.

Once you have a proc object, you can call it, well, with
the "call" method. Just change "yield @proc" to
"@proc.call" and it should work.

HTH,
Stefan

Nathan

3/9/2008 11:22:00 PM

0

Thanks, that's great.

On Mar 9, 11:02 pm, "Stefan Lang" <perfectly.normal.hac...@gmail.com>
wrote:
> 2008/3/9, Nathan <njmacin...@gmail.com>:
>
>
>
> > [...]
> > Now, the Item class, which each menu item is an object of, doesn't
> > work as expected... take a look here:http://pastie.caboo....
> > When I try to call myItem.execute!, it complains "no block given". If
> > I call myItem.execute! { 2 + 1}, then it works, but that's not what I
> > want...
> > class Item
> > attr_reader :text
>
> > def initialize(description, &proc)
> > @description = description
> > @proc = proc
> > end
>
> > def execute!
> > yield @proc
> > end
> > end
>
> Use yield when the block is _not_ stored in a variable.
> What your yield line does is this: Call the block given to
> execute! with @proc as argument.
>
> Once you have a proc object, you can call it, well, with
> the "call" method. Just change "yield @proc" to
> "@proc.call" and it should work.
>
> HTH,
> Stefan

Ken Bloom

3/10/2008 12:30:00 AM

0

On Sun, 09 Mar 2008 17:48:36 -0500, Nathan wrote:

> Hi,
> This might be something really simple, but I'm trying to write a
> console-based menu for my program. I've done this before in a procedural
> language, but I decided to try and port what I've done to ruby, because
> it'll make other parts of my program far easier. My program has a few
> menus, so it'd be nice to have re-usable code (which the procedural
> version didn't have much of).
>
> The best way I've come up with is to pass the method calls which are
> executed by each menu item as procs, but this requires passing them
> around between two objects internally. (I haven't completely decided how
> I'm going to structure the code which passes the procs... I have an
> idea, but I'll cross that when I come to it)
>
> Now, the Item class, which each menu item is an object of, doesn't work
> as expected... take a look here: http://pastie.caboo.... When I
> try to call myItem.execute!, it complains "no block given". If I call
> myItem.execute! { 2 + 1}, then it works, but that's not what I want...
>
> I've also included the Menu class, just in case anybody has any advice
> as to how to do the whole thing better.
>
> Thanks,
> -Nathan

In addition to what Stefan Lang said about @proc.call, I just wanted to
point out that there's no need to keep different classes in different
files. (This isn't Java.)

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Nathan

3/11/2008 2:23:00 AM

0

Yeah, I know. I=B4m just doing it like that for now, then I=B4ll put them
all together at the end. Thanks though.
-N

On Mar 10, 12:43 am, Ken Bloom <kbl...@gmail.com> wrote:
> On Sun, 09 Mar 2008 17:48:36 -0500, Nathan wrote:
> > Hi,
> > This might be something really simple, but I'm trying to write a
> > console-based menu for my program. I've done this before in a procedural=

> > language, but I decided to try and port what I've done to ruby, because
> > it'll make other parts of my program far easier. My program has a few
> > menus, so it'd be nice to have re-usable code (which the procedural
> > version didn't have much of).
>
> > The best way I've come up with is to pass the method calls which are
> > executed by each menu item as procs, but this requires passing them
> > around between two objects internally. (I haven't completely decided how=

> > I'm going to structure the code which passes the procs... I have an
> > idea, but I'll cross that when I come to it)
>
> > Now, the Item class, which each menu item is an object of, doesn't work
> > as expected... take a look here:http://pastie.caboo.... When I
> > try to call myItem.execute!, it complains "no block given". If I call
> > myItem.execute! { 2 + 1}, then it works, but that's not what I want...
>
> > I've also included the Menu class, just in case anybody has any advice
> > as to how to do the whole thing better.
>
> > Thanks,
> > -Nathan
>
> In addition to what Stefan Lang said about @proc.call, I just wanted to
> point out that there's no need to keep different classes in different
> files. (This isn't Java.)
>
> --Ken
>
> --
> Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
> Department of Computer Science. Illinois Institute of Technology.http://ww=
w.iit.edu/~kbloom1/