# About blocks

**URL:** <https://www.ruby-forum.com/t/about-blocks/86926>\
**Category:** Ruby\
**Created:** [January 2, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/about-blocks/86926 "2006-01-02T12:54:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surgeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Surgeon](https://www.ruby-forum.com/u/Surgeon)\
**Post date:** [January 2, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/about-blocks/86926/1 "2006-01-02T12:54:47Z")

</div>

Hi,

(This question might be answered previously. If so, I apologize.)

Can we _ALWAYS_ use plain methods instead of blocks? Yes, sometimes  
using a block may be obviously better than using a method but I mean,  
are there any situations in which the _ONLY_ solution is to use a block  
rather than a method? May someone be forced to use only and only a  
block? I ask this because I am not very intelligent and I am not sure  
that I will thoroughly understand the “block” concept.

---

<div class="post-metadata">

**Author:** ![Surgeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Surgeon](https://www.ruby-forum.com/u/Surgeon)\
**Post date:** [January 2, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/about-blocks/86926/2 "2006-01-02T17:15:31Z")

</div>

Hi –

On Mon, 2 Jan 2006, Surgeon wrote:

> Hi,
> 
> (This question might be answered previously. If so, I apologize.)
> 
> Can we _ALWAYS_ use plain methods instead of blocks? Yes, sometimes  
> using a block may be obviously better than using a method but I mean,  
> are there any situations in which the _ONLY_ solution is to use a block  
> rather than a method? May someone be forced to use only and only a  
> block? I ask this because I am not very intelligent and I am not sure  
> that I will thoroughly understand the “block” concept.

You don’t really use methods instead of blocks; you use methods –  
that is, you call methods – with or without providing a code block  
for a given method call.

The block is part of the method call syntax, along with the method  
name and the argument list. Whether or not it’s mandatory is up to  
the method (as with arguments). For example:

def x  
end

def y  
yield  
end

def z  
yield if block\_given?  
end

x # OK – no block expected or given  
x { puts “code block” } # block OK but ignored

y # error, because no block to yield to  
y { puts “code block” } # OK

z # OK – no yield will be attempted  
z { puts “code block” } # also OK, and block will execute

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![Surgeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Surgeon](https://www.ruby-forum.com/u/Surgeon)\
**Post date:** [January 2, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/about-blocks/86926/3 "2006-01-02T17:15:40Z")

</div>

Hi –

On Mon, 2 Jan 2006, Robert K. wrote:

> > not sure that I will thoroughly understand the “block” concept.  
> > attr\_accessor :comparator  
> > def each  
> > sort!(&comparator)  
> > super(&b)

I think you need either:

def each(&b)

or just:

super # this seems to propagate the block, at least in 1.8.3  
# (I think this might have changed, but not sure.)

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![Surgeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Surgeon](https://www.ruby-forum.com/u/Surgeon)\
**Post date:** [January 2, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/about-blocks/86926/4 "2006-01-02T17:15:34Z")

</div>

Surgeon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> (This question might be answered previously. If so, I apologize.)
> 
> Can we _ALWAYS_ use plain methods instead of blocks? Yes, sometimes  
> using a block may be obviously better than using a method but I mean,  
> are there any situations in which the _ONLY_ solution is to use a  
> block rather than a method? May someone be forced to use only and  
> only a block? I ask this because I am not very intelligent and I am  
> not sure that I will thoroughly understand the “block” concept.

You need blocks everywhere you need an anonymous callback function such  
as  
with iterations:

an\_array.each {|e| … }

And you need lambdas (i.e. blocks converted to an object) if you need to  
store the thing in a variable:

class SortedArray \< Array  
attr\_accessor :comparator  
def each  
sort!(&comparator)  
super(&b)  
end  
end

Does that help?

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![Surgeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Surgeon](https://www.ruby-forum.com/u/Surgeon)\
**Post date:** [January 2, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/about-blocks/86926/5 "2006-01-02T20:57:44Z")

</div>

- removed\_email\_address@domain.invalid (removed\_email\_address@domain.invalid) wrote:

> > are there any situations in which the _ONLY_ solution is to use a block  
> > the method (as with arguments). For example:  
> > end
> 
> David

really intresting lecture!

> –  
> David A. Black  
> removed\_email\_address@domain.invalid
> 
> “Ruby for Rails”, from Manning Publications, coming April 2006!  
> [Ruby for Rails](http://www.manning.com/books/black)

## So long

Michael ‘entropie’ Trommer; [http://ackro.org](http://ackro.org)

ruby -e “0.upto((a=‘njduspAhnbjm/dpn’).size-1){|x| a[x]-=1}; p  
‘mailto:’+a”
