Blocks / Closures

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?

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

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 G. II

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 2/21/06, Picklegnome [email protected] 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

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

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?

Picklegnome wrote:

Blocks are great, for all the standard stuff, iterations/list
manipulation being the foremost example. Code that would require 15
lines and a new method in Java or C++ requires a 1/2 line block and no
additional overhead such as methods. If this was the only thing blocks
did, they would still be indespencible.

However, there’s more. Blocks allow you to express a piece of logic on
its own, independant of any other construct. As an example which may or
may not help you, I am in the beginning status of a design (for work) to
use blocks within metadata to express the business rules of our client.

Now, all the business rules are expressed as methods of inline pieces of
code. Say our software manages the buying and selling of widgets.
Somewhere in the code, there is this method (it’s C++):

BOOL Widget::IsAvailableForPurchase()
{
BOOL result = FALSE;
if ( !IsBeing Repaired && /* some other conditions */ )
{
// some code goes in here that sets result
}
return result;
}

And then elsewhere, in a validate function, is this code

if (IsBeingSold() && !IsAvailableForPurchase())
{
PostError(…)
}

Very exciting.

Like any toy example, this example doesn’t seem so bad. Pretty ugly,
but not henious. Multiply by 10000, them multiply by different business
rules for 10 different clients, then imagine all the stupid code you
have to write to satisfy the compiler, then add in all the
non-business-logic code we need to mesh with the guts of the program,
then add in code rot on a decade old product. Trust me, it haunts my
dreams. The main problems are that the business rules gets mixed in
with the rest of the code, and they are expressed in a manner than looks
more natural to a programmer than to somebody in the business domain.

What I would like to do is, in some file (let’s call it
BusinessRules.rb)

validation_failure “Widget Must Be Available For Purchase To Be Sold” do
|widget|
widget.being_sold? && !widget.available_for_purchase?
end

The block should return true if the validation fails. This is ugly, but
it’s just a toy example.

The benefits here are that all your business rules are expressed outside
of code and are not sullied by other code.

My example sucks. For a better example of this concept and Domain
Specific Languages, check out Rake.

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/reason4.html


– Jim W.

On a completely unrelated note:

DÅ?a Utorok 21 Február 2006 18:08 Mike H. 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 V.
Inane as usual

David V. wrote:

 // some code goes in here that sets result

David V.
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.

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

{
David V.
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?

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
[email protected], “Malte M.”

On 2/21/06, Picklegnome [email protected] wrote:

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.

You are wise beyond your years (and so are your parents).

Of course, I had a small hope that you’d reply with “What do you mean
real name? Picklegnome IS my real name.” But that would either mean
you’re about 20 years older, or you’re a child of a movie star. :wink:

I have some examples here:
http://pine.fm/LearnToProgram/?Chapter=10

Especially the “profile” example near the end: measures the time it
takes to execute the passed-in block.

Chris