This shows the IRB output when used with the my_method listed below...
Notice the puts command in the block argument on line 1. You will notice that it just isn't there in line 6, though everything else is identical.
The important thing to realize here is:
- The return doesn't change between the two variances
- The puts may be appropriate, because the USER is placing the block there (the user in this case being the developer)
The magic with the block happens with the yield statement on line 4.
Hope this helps you with writing your methods with blocks. The other arguments are simply noise in this sample... they are there to show only that you can do things with arguments that can be determined outside of the methods direct control.
Thanks to Edo for allowing me to answer his question about method block arguments.
2 comments:
such a nice Post
Thanks.
Post a Comment