I'm new to Ruby. I normally sling code in C#. What could I do to make this simple Soundex class more Rubyesque?
class Surname
attr_accessor :value
def initialize(input)
@value = input
end
def soundex
result = ''
@value.chars.drop(1).each do |s|
number = soundex_value(s).to_s
result << number unless result[-1,1] == number
end
@value.chars.first << result.ljust(3,'0')
end
def soundex_value(s)
case s
when /[bfpv]/
1
when /[cgjkqsxz]/
2
when /[dt]/
3
when /l/
4
when /[mn]/
5
when /r/
6
else ''
end
end
end
def print_name(input)
surname = Surname.new(input)
puts(surname.value + ' => ' + surname.soundex)
end
['Smith', 'Johnson', 'Williams', 'Jones', 'Brown'].each do |s|
print_name s
end
The output is:
Smith => S530 Johnson => J525 Williams => W452 Jones => J520 Brown => B650