Question

Regarding ruby - What is block argument ? What is equivalent of block argument in java...

Regarding ruby

- What is block argument ? What is equivalent of block argument in java ?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Blocks make a lot of sense on methods that are defined on collections like arrays and hashes.

Let’s have a look at some examples with arrays.

A block that accepts an argument looks like this:

[1, 2, 3, 4, 5].each do |number|
  puts "#{number} was passed to the block"
end

And, again, this is the same as:

[1, 2, 3, 4, 5].each { |number| puts "#{number} was passed to the block" }

So, for blocks, do |number| is the same that is def add_two (number) for a method definition, except that the method wants a name while a block is anonymous: |number| and (number) both are argument lists. The first one is used for blocks, the second one for methods.

In Java, we have lambda expressions similar to block arguments.

Add a comment
Know the answer?
Add Answer to:
Regarding ruby - What is block argument ? What is equivalent of block argument in java...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT