Question

I want to puts sharp's instead of password in ruby code

puts "        found password: #{pass.tr('?','#')}"

I need as many sharp '#' characters output as characters in a password.

How to do it right?

Was it helpful?

Solution

The method .tr is intended to swap specific characters, you cannot do a wild-card match. Even if you extended it to cover many characters, there is a risk that you miss or forget a special character that is allowed in passwords on your system.

A simple variant of what you have is to use .gsub instead:

pass.gsub(/./,'#')

This uses regular expressions to find groups of characters to swap. The simple Regexp /./ matches any single character. The Ruby core documentation on regular expressions includes a brief introduction, in case you have not used them much before.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top