From: "Jan E." Date: 2012-05-18T12:20:50+09:00 Subject: Re: what is going wrong here? The case of the noob not understanding initialize This looks good. :-) Since you already said that the character tests follow the same pattern, you might wanna think about writing only one generalized method. For example, you could put the character sets and the corresponding "scores" in an array and then let a method iterate over it. You don't even have to write the actual characters down but use regular expressions instead: /\p{Lower}/ This regular expression matches any string with a lower case letter (of any language) in it. If you only want the latin letters from a to z, you can instead use the pattern /[a-z]/ This narrows the test down to one line: password =~ pattern As a complete example: #------------------------------------------ class Entropy # the scores are just made up CHARACTER_SCORES = { # special characters # = printable ASCII characters which are not alphanumeric /[\p{ASCII}&&\p{Graph}&&\p{^Alnum}]/ => 11, # uppercase letters /[A-Z]/ => 22, # lowercase letter /[a-z]/ => 33, # digits /[0-9]/ => 44 } def test_characters password CHARACTER_SCORES.inject 0 do |sum, (pattern, score)| password =~ pattern ? sum + score : sum end end end #------------------------------------------ By the way, most of the time you don't need lowlevel structures like the while statement. Ruby has a lot of iterators which do the same thing in a lot less lines. For example, you could rewrite the dictionary method to this: #------------------------------------------ def contains_dictionary_word?(password) entropy_bits = 0 passed_dictionary_test = File.foreach("#{RELATIVEPATH}/data/dictionary").any? do |line| password.include? line.chop end entropy_bits = 6 if passed_dictionary_test and password.length > 4 return entropy_bits end #------------------------------------------ The "while gets" is replaced with "File.foreach". And the "break false" is replaced with the "any?" iterator. On last thing: You should be careful with method names. A method called "contains_dictionary_word?" looks all like it returns either true or false. It it returns a score instead, this may lead to confusion. So I'd rather call it "test_dictionary" or so. -- Posted via http://www.ruby-forum.com/.