문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top