When to use blocks?

When to use blocks and yield in ruby code.A practical example will be
appreciated.

Thank you!!

Yours sincerely,
Surya Poojary

Did you try some basic internet research? It's a very core part of the ruby
functionality, there are lots of examples all over the internet.

···

On Tue, Nov 14, 2017 at 5:19 PM Surya Poojary <suryapjr@gmail.com> wrote:

When to use blocks and yield in ruby code.A practical example will be
appreciated.

Thank you!!

Yours sincerely,
Surya Poojary

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--

- Raj

Hi Surya,

it's a good beginners question to ask, don't worry. Such basics are not easily gleamed off the internet.

You use blocks to pass functionality around. Normal arguments are data, but with blocks you can pass functionality to another function.

THE classic ruby example is the enumerable module, check that out. Let's take a collect over an array.
Let's say you want the squares of an array of integers

numbers = [1,2,3,4]
squares = numbers.collect{ |x| x * x }
=> [ 1 , 4 , 9 , 16]

The block {|x| x*x} , gets passed to the collect method. Collect in turn "apples" the block to every (each!) element of the array, and stores the return values in a new array. This "applying" is called yielding, which basically is calling or invoking the block (possibly with an argument |x| )
There are lots of methods built of this "apply to each" idea, check out enumerable. Once you get used to this (otherwise also know as functional style) of programming it is very useful for lots of other things too.

Torsten

···

Surya Poojary <mailto:suryapjr@gmail.com>
15 November 2017 at 3.19

When to use blocks and yield in ruby code.A practical example will be appreciated.

Thank you!!

Yours sincerely,
Surya Poojary

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

In C language, blocks could be regarded as something similar to functions pointers
passed as arguments to other functions.
Using blocks you tell a function to execute another function in its (of the calling function)
particular context.

···

--

Regards,
and let's make better business with http://www.redmine.org

Ivan Cenov

Yes..a block stops the flow of execution in a function and returns it after
the block is executed

···

On 15-Nov-2017 6:53 AM, "Raj Sahae" <rajsahae@gmail.com> wrote:

Did you try some basic internet research? It's a very core part of the
ruby functionality, there are lots of examples all over the internet.

On Tue, Nov 14, 2017 at 5:19 PM Surya Poojary <suryapjr@gmail.com> wrote:

When to use blocks and yield in ruby code.A practical example will be
appreciated.

Thank you!!

Yours sincerely,
Surya Poojary

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--

- Raj

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

In a way, yes. But I'd still consider that a bad description of the
feature. I wrote about this long ago
https://web.archive.org/web/20160315055701/http://blog.rubybestpractices.com/posts/rklemme

robert

···

On Wed, Nov 15, 2017 at 8:55 AM, Surya Poojary <suryapjr@gmail.com> wrote:

Yes..a block stops the flow of execution in a function and returns it after
the block is executed

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Hi Surya,

I think it is an excellent question, and hopefully there will be lots of
answers. Here is mine:

  Using a block helps you to create a function/method that can be modified
"as you use it" - in other words, customize
a function/method on the fly. In these invocations, you specify the number
of parameters a block can take and
prescribe a rule which, in combination with other expressions/variables
written inside the function, help to create something new
that was not expressed in the original function/method.

For example, the method each is just used to enumerate the contents of a
list. A block helps to create new functionality, maybe add
a prefix to the contents of the list that each will operate on: a =
["one","two"] ; a.each {|e| puts "hello "+e}.

There is much material on the web dealing with blocks including
https://mixandgo.com/blog/mastering-ruby-blocks-in-less-than-5-minutes

cheers,
saji

Saji N Hameed,
Environmental Informatics,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
email: saji@u-aizu.ac.jp
url: http://enformtk.u-aizu.ac.jp
bib: Web of Science
code: sajinh (Saji Hameed) · GitHub

···

On Wed, Nov 15, 2017 at 4:55 PM, Surya Poojary <suryapjr@gmail.com> wrote:

Yes..a block stops the flow of execution in a function and returns it
after the block is executed
On 15-Nov-2017 6:53 AM, "Raj Sahae" <rajsahae@gmail.com> wrote:

Did you try some basic internet research? It's a very core part of the
ruby functionality, there are lots of examples all over the internet.

On Tue, Nov 14, 2017 at 5:19 PM Surya Poojary <suryapjr@gmail.com> wrote:

When to use blocks and yield in ruby code.A practical example will be
appreciated.

Thank you!!

Yours sincerely,
Surya Poojary

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--

- Raj

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Thank you Sajid and Robert for the clear revert!

···

On 15-Nov-2017 1:39 PM, "Robert Klemme" <shortcutter@googlemail.com> wrote:

On Wed, Nov 15, 2017 at 8:55 AM, Surya Poojary <suryapjr@gmail.com> wrote:
> Yes..a block stops the flow of execution in a function and returns it
after
> the block is executed

In a way, yes. But I'd still consider that a bad description of the
feature. I wrote about this long ago
https://web.archive.org/web/20160315055701/http://blog\.
rubybestpractices.com/posts/rklemme

robert

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

There is a book recommendation, "Meta-programming Ruby 2". It's a very good book about everything on meta-programming of ruby. Also, It explains the fundamentals of both block and lambda, not tell you when and how, but why.

Happy Reading.

···

Sent from Outlook.com.
________________________________
From: ruby-talk <ruby-talk-bounces@ruby-lang.org> on behalf of Surya Poojary <suryapjr@gmail.com>
Sent: Wednesday, November 15, 2017 9:19 AM
To: ruby-talk@ruby-lang.org
Subject: When to use blocks?

When to use blocks and yield in ruby code.A practical example will be appreciated.

Thank you!!

Yours sincerely,
Surya Poojary

The block is also a closure.

发自我的 iPhone

在 2017年11月15日,18:40,Ivan Cenov <i_cenov@botevgrad.com<mailto:i_cenov@botevgrad.com>> 写道:

In C language, blocks could be regarded as something similar to functions pointers
passed as arguments to other functions.
Using blocks you tell a function to execute another function in its (of the calling function)
particular context.

···

--

Regards,
and let's make better business with http://www.redmine.org

Ivan Cenov

thank you for recommending

···

On Wed, Nov 15, 2017 at 11:57 AM, 智 杨 <yangzhi_java_work@outlook.com> wrote:

There is a book recommendation, "Meta-programming Ruby 2". It's a very
good book about everything on meta-programming of ruby. Also, It explains
the fundamentals of both block and lambda, not tell you when and how, but
why.

Happy Reading.

Sent from Outlook.com.
------------------------------
*From:* ruby-talk <ruby-talk-bounces@ruby-lang.org> on behalf of Surya
Poojary <suryapjr@gmail.com>
*Sent:* Wednesday, November 15, 2017 9:19 AM
*To:* ruby-talk@ruby-lang.org
*Subject:* When to use blocks?

When to use blocks and yield in ruby code.A practical example will be
appreciated.

Thank you!!

Yours sincerely,
Surya Poojary

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;