[lnkForumImage]
TotalShareware - Download Free Software

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


 

Corey Konrad

3/16/2007 10:10:00 PM

this works fine:

def printer()
yield "ball"
end

printer {|word_one| puts word_one}


but this does not:

def printer(arg)
yield arg
end

printer("ball")
printer {|word| puts word}

why is that, cant yield be used with arguments?

thanks

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

11 Answers

Brian Candler

3/16/2007 10:17:00 PM

0

On Sat, Mar 17, 2007 at 07:10:26AM +0900, Corey Konrad wrote:
> this works fine:
>
> def printer()
> yield "ball"
> end
>
> printer {|word_one| puts word_one}
>
>
> but this does not:
>
> def printer(arg)
> yield arg
> end
>
> printer("ball")
> printer {|word| puts word}
>
> why is that, cant yield be used with arguments?

It can. Your second function 'printer' takes one argument plus one block.
However in the first case you were calling it without a block, and in the
second case you were calling it without an argument.

What you need is:

printer("ball") {|word| puts word}

matthew.moss.coder

3/16/2007 10:25:00 PM

0

> why is that, cant yield be used with arguments?

It can, but you need to use it correctly:


> def printer(arg)
> yield arg
> end

Good.


> printer("ball")
> printer {|word| puts word}

Not so good. Try:

printer("ball") { |word| puts word }

Corey Konrad

3/16/2007 10:36:00 PM

0

Brian Candler wrote:
> On Sat, Mar 17, 2007 at 07:10:26AM +0900, Corey Konrad wrote:
>>
>> def printer(arg)
>> yield arg
>> end
>>
>> printer("ball")
>> printer {|word| puts word}
>>
>> why is that, cant yield be used with arguments?
>
> It can. Your second function 'printer' takes one argument plus one
> block.
> However in the first case you were calling it without a block, and in
> the
> second case you were calling it without an argument.
>
> What you need is:
>
> printer("ball") {|word| puts word}


oh ok, my understanding for some reason was that the method was going to
remmeber that i set arg to "ball" so that when i used printer with the
block that "ball" would still be in arg. So is that why it isnt working,
is because when i call the printer method the second time the value
stored in arg is destroyed?

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

Chad Perrin

3/16/2007 10:43:00 PM

0

On Sat, Mar 17, 2007 at 07:36:21AM +0900, Corey Konrad wrote:
> Brian Candler wrote:
> > On Sat, Mar 17, 2007 at 07:10:26AM +0900, Corey Konrad wrote:
> >>
> >> def printer(arg)
> >> yield arg
> >> end
> >>
> >> printer("ball")
> >> printer {|word| puts word}
> >>
> >> why is that, cant yield be used with arguments?
> >
> > It can. Your second function 'printer' takes one argument plus one
> > block.
> > However in the first case you were calling it without a block, and in
> > the
> > second case you were calling it without an argument.
> >
> > What you need is:
> >
> > printer("ball") {|word| puts word}
>
> oh ok, my understanding for some reason was that the method was going to
> remmeber that i set arg to "ball" so that when i used printer with the
> block that "ball" would still be in arg. So is that why it isnt working,
> is because when i call the printer method the second time the value
> stored in arg is destroyed?

I'm just guessing here, but . . .

It looks like you're expecting something akin to class behavior from a
method. Perhaps what you want to do is something more like this:

class Printer
def initialize(arg)
@arg = arg
end

def iterator_thingie
yield @arg
end
end

printer = Printer.new("ball")
printer.iterator_thingie {|word| puts word}


Then again, maybe that's not the kind of behavior you want.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
print substr("Just another Perl hacker", 0, -2);

Corey Konrad

3/16/2007 10:48:00 PM

0

Chad Perrin wrote:
> On Sat, Mar 17, 2007 at 07:36:21AM +0900, Corey Konrad wrote:
>> >> why is that, cant yield be used with arguments?
>>
>> oh ok, my understanding for some reason was that the method was going to
>> remmeber that i set arg to "ball" so that when i used printer with the
>> block that "ball" would still be in arg. So is that why it isnt working,
>> is because when i call the printer method the second time the value
>> stored in arg is destroyed?
>
> I'm just guessing here, but . . .
>
> It looks like you're expecting something akin to class behavior from a
> method. Perhaps what you want to do is something more like this:
>
> class Printer
> def initialize(arg)
> @arg = arg
> end
>
> def iterator_thingie
> yield @arg
> end
> end
>
> printer = Printer.new("ball")
> printer.iterator_thingie {|word| puts word}
>
>
> Then again, maybe that's not the kind of behavior you want.

its not that i am looking to get any specific result its that i am
trying to understand what is going on behind the scenes so that i can
understand why what i did, did not work not just that it in fact did not
work.

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

Corey Konrad

3/16/2007 10:58:00 PM

0

Chad Perrin wrote:
> On Sat, Mar 17, 2007 at 07:36:21AM +0900, Corey Konrad wrote:
>> >> why is that, cant yield be used with arguments?
>>
>> oh ok, my understanding for some reason was that the method was going to
>> remmeber that i set arg to "ball" so that when i used printer with the
>> block that "ball" would still be in arg. So is that why it isnt working,
>> is because when i call the printer method the second time the value
>> stored in arg is destroyed?
>
> I'm just guessing here, but . . .
>
> It looks like you're expecting something akin to class behavior from a
> method. Perhaps what you want to do is something more like this:
>
> class Printer
> def initialize(arg)
> @arg = arg
> end
>
> def iterator_thingie
> yield @arg
> end
> end
>
> printer = Printer.new("ball")
> printer.iterator_thingie {|word| puts word}
>
>
> Then again, maybe that's not the kind of behavior you want.

what i am basically trying to understand is:

When i do printer("ball") i am putting the string ball in the arg
variable of the printer method.

When i call the printer method with the block afterwards what happened
to my string i stored in arg? Was it destroyed or when i called the
printer method for the second time did ruby create a whole new arg
variable with nothing in it and thats why the way i did it didnt work?
Do you know what i mean i am trying to understand why what i did didnt
work.


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

Gary Wright

3/16/2007 11:16:00 PM

0


On Mar 16, 2007, at 6:58 PM, Corey Konrad wrote:
>> Then again, maybe that's not the kind of behavior you want.
>
> what i am basically trying to understand is:
>
> When i do printer("ball") i am putting the string ball in the arg
> variable of the printer method.

Forget the block for a moment. Your question pertains to method
arguments with or without an associated block.

Next, don't think of a variable as a container ("putting the
string 'ball' in the arg). Variables are not containers in Ruby
they are names. Think of sticking a post-it note on 'ball' and
written on the post-it note is 'arg'.

The post-it note gets created and stuck on 'ball' when you
call the method. The post-it note gets removed from 'ball'
when the method returns. Within the method the identifier
'arg' can be used to identify the string (in this case 'ball').
Outside the method 'arg' is meaningless since the post-it note
labeled 'arg' has been discarded.

When you call a method a second (or third or fourth...) time,
a new post-it note is created and stuck on the object and
'arg' is once again written on the post-it note and can be
used to identify the object you provided.

printer('ball') # within printer, arg refers to 'ball'
# no post-it note 'arg' between calls to printer
printer('baseball') # within printer, arg refers to 'baseball'
# no post-it note 'arg' between calls to printer
printer('football') # within printer, arg refers to 'football'




Gary Wright




Chad Perrin

3/17/2007 12:12:00 AM

0

On Sat, Mar 17, 2007 at 07:58:15AM +0900, Corey Konrad wrote:
> Chad Perrin wrote:
> > On Sat, Mar 17, 2007 at 07:36:21AM +0900, Corey Konrad wrote:
> >> >> why is that, cant yield be used with arguments?
> >>
> >> oh ok, my understanding for some reason was that the method was going to
> >> remmeber that i set arg to "ball" so that when i used printer with the
> >> block that "ball" would still be in arg. So is that why it isnt working,
> >> is because when i call the printer method the second time the value
> >> stored in arg is destroyed?
> >
> > I'm just guessing here, but . . .
> >
> > It looks like you're expecting something akin to class behavior from a
> > method. Perhaps what you want to do is something more like this:
> >
> > class Printer
> > def initialize(arg)
> > @arg = arg
> > end
> >
> > def iterator_thingie
> > yield @arg
> > end
> > end
> >
> > printer = Printer.new("ball")
> > printer.iterator_thingie {|word| puts word}
> >
> >
> > Then again, maybe that's not the kind of behavior you want.
>
> what i am basically trying to understand is:
>
> When i do printer("ball") i am putting the string ball in the arg
> variable of the printer method.
>
> When i call the printer method with the block afterwards what happened
> to my string i stored in arg? Was it destroyed or when i called the
> printer method for the second time did ruby create a whole new arg
> variable with nothing in it and thats why the way i did it didnt work?
> Do you know what i mean i am trying to understand why what i did didnt
> work.

Methods are not persistent things -- they are actions taken by objects.
When you call a method that you define outside of any class, it's
actually a method of the Self object (basically, a method of your
program). Because actions do not maintain state (data, variables, what
have you), variables within methods go away when the methods are not in
use. A method is an ephemeral, fleeting thing. It's an occurrence, in
a way, rather than a thing.

A method, in fact, is much like a function in that respect (if you're
familiar with functions from other languages). You put something in,
you get something out.

Blocks can be used to maintain state, but to do so you need to assign
them as objects to some kind of name/variable/thing that is outside of a
method. That, of course, is because a block returned from a method and
stuck in a variable is basically the same as a closure in any other
language (that supports closures).

Hopefully that helps clear things up for you, and give you a glimpse of
other, sorta related topics in the language.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"The measure on a man's real character is what he would do
if he knew he would never be found out." - Thomas McCauley

dblack

3/17/2007 3:51:00 AM

0

Hi --

On 3/16/07, Corey Konrad <0011@hush.com> wrote:
> Brian Candler wrote:
> > On Sat, Mar 17, 2007 at 07:10:26AM +0900, Corey Konrad wrote:
> >>
> >> def printer(arg)
> >> yield arg
> >> end
> >>
> >> printer("ball")
> >> printer {|word| puts word}
> >>
> >> why is that, cant yield be used with arguments?
> >
> > It can. Your second function 'printer' takes one argument plus one
> > block.
> > However in the first case you were calling it without a block, and in
> > the
> > second case you were calling it without an argument.
> >
> > What you need is:
> >
> > printer("ball") {|word| puts word}
>
>
> oh ok, my understanding for some reason was that the method was going to
> remmeber that i set arg to "ball" so that when i used printer with the
> block that "ball" would still be in arg. So is that why it isnt working,
> is because when i call the printer method the second time the value
> stored in arg is destroyed?

The two calls to printer are unrelated to each other. Each of them
uses a local variable 'arg', but they're two different variables.

The code block is, syntactically, part of the method call. So if
there's no block for a given method call, that's that.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning...)
(See what readers are saying! http://www.r.../r...)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.r...)

Bertram Scharpf

3/17/2007 5:38:00 AM

0

Am Samstag, 17. Mär 2007, 07:10:26 +0900 schrieb Corey Konrad:
> def printer(arg)
> yield arg
> end
>
> printer("ball")
> printer {|word| puts word}

How did you get this working? Both `printer' calls will
fail. Please run your code before you post it or at least
include the error message.

Please don't mind,

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...