Ruby - Passing Blocks To Methods

Posted by Chris Bunch on Stack Overflow See other posts from Stack Overflow or by Chris Bunch
Published on 2008-11-11T17:31:55Z Indexed on 2010/05/21 17:50 UTC
Read the original article Hit count: 205

Filed under:
|
|

I'm trying to do Ruby password input with the Highline gem and since I have the user input the password twice, I'd like to eliminate the duplication on the blocks I'm passing in. For example, a simple version of what I'm doing right now is:

new_pass = ask("Enter your new password: ") { |prompt| prompt.echo = false }
verify_pass = ask("Enter again to verify: ") { |prompt| prompt.echo = false }

And what I'd like to change it to is something like this:

foo = Proc.new { |prompt| prompt.echo = false }
new_pass = ask("Enter your new password: ") foo
verify_pass = ask("Enter again to verify: ") foo

Which unfortunately doesn't work. What's the correct way to do this?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about blocks