[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Blocks / Closures

Picklegnome

2/21/2006 3:06:00 PM

I've just been looking into Ruby in the last few days, and I must say it
does look promising. However, many of the tutorials and other resources I've
been looking at use blocks. Having done some research, I'm beginning to
understand what they are and how they are used. However, I've only come
across examples that use them for iteration and keeping a static variable
hidden. Is there something I'm missing? Can blocks be used for non-iterative
functions?

12 Answers

James Gray

2/21/2006 3:16:00 PM

0

On Feb 21, 2006, at 9:08 AM, Picklegnome wrote:

> I've just been looking into Ruby in the last few days, and I must
> say it
> does look promising. However, many of the tutorials and other
> resources I've
> been looking at use blocks. Having done some research, I'm
> beginning to
> understand what they are and how they are used. However, I've only
> come
> across examples that use them for iteration and keeping a static
> variable
> hidden. Is there something I'm missing? Can blocks be used for non-
> iterative
> functions?

Sure. File::open uses a block for resource clean-up (auto file
closing). The standard PStore library uses them for transactions.

Hope that helps.

James Edward Gray II



Explosiv0SX

2/21/2006 3:21:00 PM

0

Pick-up the latest edition of Programming Ruby (ISBN:0974514055). As
a recent newcomer to Ruby, I must say, this is an indispensable
resource. Check out some of the gui libraries -- there you'll see
some cool examples on how blocks/closures can support event handling.
This is one topic I'd like to see covered in a future edition of the
above mentioned book as there is little documentation outside of
sample code available.

Rick

On Feb 21, 2006, at 10:08 AM, Picklegnome wrote:

> I've just been looking into Ruby in the last few days, and I must
> say it
> does look promising. However, many of the tutorials and other
> resources I've
> been looking at use blocks. Having done some research, I'm
> beginning to
> understand what they are and how they are used. However, I've only
> come
> across examples that use them for iteration and keeping a static
> variable
> hidden. Is there something I'm missing? Can blocks be used for non-
> iterative
> functions?
>
>



fred.grott@gmail.com

2/21/2006 4:33:00 PM

0


There is a presentation you might want to read..

WorkinOnTheRailRoad


I believe its in the ruby-doc.org downloads..






Picklegnome wrote:
> I've just been looking into Ruby in the last few days, and I must say it
> does look promising. However, many of the tutorials and other resources
> I've
> been looking at use blocks. Having done some research, I'm beginning to
> understand what they are and how they are used. However, I've only come
> across examples that use them for iteration and keeping a static
> variable
> hidden. Is there something I'm missing? Can blocks be used for
> non-iterative
> functions?


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


Patrick Hurley

2/21/2006 4:51:00 PM

0

On 2/21/06, Picklegnome <picklegnome@nerdshack.com> wrote:
> Can blocks be used for non-iterative
> functions?

In addition to what has already been mentioned, blocks are useful
whenever you want to abstract an action out of a routine or delay
evaluation. For example, they can be handy for call backs (in GUI
programming), developing threaded code, state machine call backs, etc.

pth


Malte Milatz

2/21/2006 5:00:00 PM

0

Picklegnome wrote:
> Can blocks be used for non-iterative functions?

For example, Hash.new can be given a block in which you provide a default
value. Assuming that there is a class called Something:

h = Hash.new { Something.new }
a, b, c = h[0], h[0], h[1]

a, b and c will contain three non-equal instances of Something, because
every time h#[] is called, the block will be called.

Any chance that we see your real name?

Malte

Jim Weirich

2/21/2006 5:32:00 PM

0

Picklegnome wrote:
> I've just been looking into Ruby in the last few days, and I must say it
> does look promising. However, many of the tutorials and other resources
> I've
> been looking at use blocks. Having done some research, I'm beginning to
> understand what they are and how they are used. However, I've only come
> across examples that use them for iteration and keeping a static
> variable
> hidden. Is there something I'm missing? Can blocks be used for
> non-iterative
> functions?

Here's some ideas:
http://onestepback.org/articles/invitationtoruby/re...

--
-- Jim Weirich

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


David Vallner

2/21/2006 7:21:00 PM

0

On a completely unrelated note:

Dna Utorok 21 Február 2006 18:08 Mike Harris napísal:
> BOOL Widget::IsAvailableForPurchase()
> {
> BOOL result = FALSE;
> if ( !IsBeing Repaired && /* some other conditions */ )
> {
> // some code goes in here that sets result
> }
> return result;
> }
>

People STILL don't use C++98 bools? Or is that the gods know why still popular
VS6.0 compiler rearing its ugly head, again?

David Vallner
Inane as usual


Mike Harris

2/21/2006 8:45:00 PM

0

David Vallner wrote:

>On a completely unrelated note:
>
>Dna Utorok 21 Február 2006 18:08 Mike Harris napísal:
>
>
>>BOOL Widget::IsAvailableForPurchase()
>>{
>> BOOL result = FALSE;
>> if ( !IsBeing Repaired && /* some other conditions */ )
>> {
>> // some code goes in here that sets result
>> }
>> return result;
>>}
>>
>>
>>
>
>People STILL don't use C++98 bools? Or is that the gods know why still popular
>VS6.0 compiler rearing its ugly head, again?
>
>David Vallner
>Inane as usual
>
>
>
>
Program was VS6.0 until 9 months ago. The insides of this program are
so decrepit that switching to using real bool's is waaaaaaaaaaaay down
on the list of things to do.


Logan Capaldo

2/21/2006 8:53:00 PM

0


On Feb 21, 2006, at 3:44 PM, Mike Harris wrote:

> David Vallner wrote:
>
>> On a completely unrelated note:
>>
>> Dna Utorok 21 Február 2006 18:08 Mike Harris napísal:
>>
>>> BOOL Widget::IsAvailableForPurchase()
>>> {
>>> BOOL result = FALSE;
>>> if ( !IsBeing Repaired && /* some other conditions */ )
>>> {
>>> // some code goes in here that sets result
>>> }
>>> return result;
>>> }
>>>
>>>
>>
>> People STILL don't use C++98 bools? Or is that the gods know why
>> still popular VS6.0 compiler rearing its ugly head, again?
>>
>> David Vallner
>> Inane as usual
>>
>>
>>
> Program was VS6.0 until 9 months ago. The insides of this program
> are so decrepit that switching to using real bool's is
> waaaaaaaaaaaay down on the list of things to do.

Search and replace? <g>



Picklegnome

2/22/2006 2:23:00 AM

0

Thanks for all your help!

As for my real name, I must apologize. I know it's bad etiquette, but I am
required to wait for a few more years (at least until I'm out of the house,
if not 18) by my parents. I personally don't see a problem with it, but I
don't feel it's inappropriate for them to ask that of me. Again, my
apologies.

Once more, thank you all for your recommendations and examples. They're
quite helpful!

Picklegnome



On 2/21/06 9:59 AM, in article
pan.2006.02.21.16.59.45.666422@gmx-topmail.de, "Malte Milatz"
<malteNOSPAM@gmx-topmail.de> wrote:

> Picklegnome wrote:
>> Can blocks be used for non-iterative functions?
>
> For example, Hash.new can be given a block in which you provide a default
> value. Assuming that there is a class called Something:
>
> h = Hash.new { Something.new }
> a, b, c = h[0], h[0], h[1]
>
> a, b and c will contain three non-equal instances of Something, because
> every time h#[] is called, the block will be called.
>
> Any chance that we see your real name?
>
> Malte
>