3. Learning to Yield


I think i got the basic idea of that lesson. I could also play around with the new command yield, but i couldn't understand this very first sentence wich was: " Why do some methods accept a block and others don't? "
What did they meant by some methods do not accepts blocks i never saw an example for this. And yet how it is realated with yield? I mean if a method totally can not run a block of code how could yield help? Please help me, and give me an example when a method does not accept a block. Looking forward to your help Have a great day


For the first part of your question, I think an example of a method that doesn't accept a block might be something like nil?

name = "kat"
#=> returns false

This method just returns true or false - you don't pass it a block to execute.

For the second part, "how it is realated with yield?" -

I think the idea is just to show you that some built-in Ruby methods accept blocks, and you can write your own methods that behave the same way (executing a block) using yield.

I do not think they were trying to imply that you could use yield with methods that don't accept blocks to make them accept blocks.


I'd love to know more about this too. Maybe as I get deeper into the lesson it will be explained. Thanks for your contribution katlandreth.