About blocks

S

Surgeon

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.
 
R

Robert Klemme

Surgeon said:
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
 
M

Michael 'entropie' Trommer

--IS0zKkzwUGydFO0o
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi --
=20
On Mon, 2 Jan 2006, Surgeon wrote:
=20
=20
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.
=20
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:
=20
def x
end
=20
def y
yield
end
=20
def z
yield if block_given?
end
=20
x # OK -- no block expected or given
x { puts "code block" } # block OK but ignored
=20
y # error, because no block to yield to
y { puts "code block" } # OK
=20
z # OK -- no yield will be attempted
z { puts "code block" } # also OK, and block will execute
=20
=20
David

really intresting lecture!
=20
--=20
David A. Black
(e-mail address removed)
=20
"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
=20
So long
--
Michael 'entropie' Trommer; http://ackro.org

ruby -e "0.upto((a=3D'njduspAhnbjm/dpn').size-1){|x| a[x]-=3D1}; p 'mailto:=
'+a"

--IS0zKkzwUGydFO0o
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDuYVuBBd8ye5RguQRAgxNAJ9uWojyehmgGuHCTxCeJHEy65Zg8ACgtOd2
uN72Zgflz66OlQQ3yN09hTg=
=t+4+
-----END PGP SIGNATURE-----

--IS0zKkzwUGydFO0o--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top