I've been coding in Ruby for many years and until last year found myself that I don't use blocks in the code I write in a daily basis xD
I don't feel bad for that but I do wonder what's the real use case of blocks when we create our own functions? I mean, I know how to use the ones in the Ruby API but I don't see cases where a function I wrote would benefit from a block.
Example, I know I can do:
result=ary.map{|item|# some calculation }
but when I write something like this
defmy_function(args)end
I've never added a block to the functions I write xD
I'd like to extent the question to you to know your opinion :)
Thank you for your feedback and an interesting question 🙂
I also do not write custom methods that yield blocks too often. One example that currently comes to my mind is when I was working with Net::SFTP to perform various operations for sftp files: download, upload, rename, get all files.
We also had different ways to connect to SFTP server - via user/password or via ssh/no password.
One way to make it less redundant would be to create a method that accepts a block (with syntactic sugar parameter &block):
# Some pseudocode will be heredefflexible_sftp(&block)ifuse_ssh?returnNet::SFTP.start(server,username,{keys: [ssh_key_path]},&block)elsereturnNet::SFTP.start(server,username,{password: password,port: port},&block)endendflexible_sftpdo|sftp|sftp.upload!("/local/file.tgz","/remote/file.tgz")end
Of course, it might be possible to avoid using a custom method here and instead pass some sort of argument hash or object into the method. But the code above is one way to solve the problem.
Another time I used yield is when I used to run performance metrics with RubyProf
I've been asking this question to every one I can (last's year RubyConfColombia I even asked Aaron Patterson 😅) and the answers is almost the same: "not [too] often".
Your examples are really good and this was precisely why I asked. Getting to see blocks being used outside the Ruby API is a way to experience their benefit or possibilities.
Thanks again!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Maryna, nice explanation here.
I've been coding in Ruby for many years and until last year found myself that I don't use blocks in the code I write in a daily basis xD
I don't feel bad for that but I do wonder what's the real use case of blocks when we create our own functions? I mean, I know how to use the ones in the Ruby API but I don't see cases where a function I wrote would benefit from a block.
Example, I know I can do:
but when I write something like this
I've never added a block to the functions I write xD
I'd like to extent the question to you to know your opinion :)
Hi Francisco,
Thank you for your feedback and an interesting question 🙂
I also do not write custom methods that yield blocks too often. One example that currently comes to my mind is when I was working with Net::SFTP to perform various operations for sftp files: download, upload, rename, get all files.
We also had different ways to connect to SFTP server - via user/password or via ssh/no password.
It could result into something like this
One way to make it less redundant would be to create a method that accepts a block (with syntactic sugar parameter
&block
):Of course, it might be possible to avoid using a custom method here and instead pass some sort of argument hash or object into the method. But the code above is one way to solve the problem.
Another time I used
yield
is when I used to run performance metrics with RubyProfAwesome. Thanks for your answer.
I've been asking this question to every one I can (last's year RubyConfColombia I even asked Aaron Patterson 😅) and the answers is almost the same: "not [too] often".
Your examples are really good and this was precisely why I asked. Getting to see blocks being used outside the Ruby API is a way to experience their benefit or possibilities.
Thanks again!