Forum: Ruby Blocks / Closures

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1b68ea01b10478309415fc330f3a64c0?d=identicon&s=25 Picklegnome (Guest)
on 2006-02-21 16:08
(Received via mailing list)
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?
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-02-21 16:17
(Received via mailing list)
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 Gray II
Ab5218ff55bc8f5ce05342fe72203a3f?d=identicon&s=25 Explosiv0SX (Guest)
on 2006-02-21 16:24
(Received via mailing list)
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
B86bf4bc0bad3872deec1f92d17204bb?d=identicon&s=25 Fred Grott (shareme)
on 2006-02-21 17:32
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?
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 Patrick Hurley (Guest)
on 2006-02-21 17:53
(Received via mailing list)
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
386f25c3bb2696a57b79e55a649769fb?d=identicon&s=25 Malte Milatz (Guest)
on 2006-02-21 18:05
(Received via mailing list)
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
97cbca14d17274370cce501bbea7980a?d=identicon&s=25 Mike Harris (Guest)
on 2006-02-21 18:08
(Received via mailing list)
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.
10d4acbfdaccb4eee687a428ca00a5d8?d=identicon&s=25 Jim Weirich (weirich)
on 2006-02-21 18:31
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/r...

--
-- Jim Weirich
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-02-21 20:23
(Received via mailing list)
On a completely unrelated note:

DÅ?a 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
97cbca14d17274370cce501bbea7980a?d=identicon&s=25 Mike Harris (Guest)
on 2006-02-21 21:46
(Received via mailing list)
David Vallner wrote:

>>      // some code goes in here that sets result
>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.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-02-21 21:56
(Received via mailing list)
On Feb 21, 2006, at 3:44 PM, Mike Harris wrote:

>>>  {
>> 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>
1b68ea01b10478309415fc330f3a64c0?d=identicon&s=25 Picklegnome (Guest)
on 2006-02-22 03:24
(Received via mailing list)
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"
Ff63c03fd68754adbadd2c6314646bef?d=identicon&s=25 Bill Guindon (agorilla)
on 2006-02-22 04:01
(Received via mailing list)
On 2/21/06, Picklegnome <picklegnome@nerdshack.com> 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. ;)
B464ff27d7d3b0750a7bfe2717c49d2d?d=identicon&s=25 Chris Pine (Guest)
on 2006-02-22 16:28
(Received via mailing list)
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
This topic is locked and can not be replied to.