Forum: Ruby About blocks

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.
73803e9b62fe9b6499db6a7ca87c909c?d=identicon&s=25 Surgeon (Guest)
on 2006-01-02 12:54
(Received via mailing list)
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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-02 17:15
(Received via mailing list)
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
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-02 17:15
(Received via mailing list)
Surgeon <biyokuantum@gmail.com> 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-02 17:15
(Received via mailing list)
Hi --

On Mon, 2 Jan 2006, Robert Klemme 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
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
453bbc23c591908cbc5f8f9e0145cc33?d=identicon&s=25 Michael 'entropie' Trommer (Guest)
on 2006-01-02 20:57
(Received via mailing list)
* dblack@wobblini.net (dblack@wobblini.net) 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
> dblack@wobblini.net
>
> "Ruby for Rails", from Manning Publications, coming April 2006!
> http://www.manning.com/books/black
>
So long
--
Michael 'entropie' Trommer;  http://ackro.org

ruby -e "0.upto((a='njduspAhnbjm/dpn').size-1){|x| a[x]-=1}; p
'mailto:'+a"
This topic is locked and can not be replied to.