My code is:
def LetterChanges(str)
str.each_char {|x| print x.next!}
end
LetterChanges("hello")
Which returns:
"ifmmp" => "hello"
How do I get it to only return "ifmmp"
? Any help would be greatly appreciated.
My code is:
def LetterChanges(str)
str.each_char {|x| print x.next!}
end
LetterChanges("hello")
Which returns:
"ifmmp" => "hello"
How do I get it to only return "ifmmp"
? Any help would be greatly appreciated.
"hello".gsub(/./, &:next)
# => "ifmmp"
def LetterChanges(str)
str.chars.map(&:next).join("")
end
LetterChanges("hello")
# => "ifmmp"
or
def LetterChanges(str)
str.size.times{|i| str[i] = str[i].next }
str
end
LetterChanges("hello")
# => "ifmmp"
puts str;
This should do it for you.
The solution is easy:
def LetterChanges(str)
puts str.chars.map(&:next).join
end
but I'd suggest you to refactor it to let puts
out. This way you don't hardcode the print of the value and you just make it return the string so that the user of the method can do whatever he wants with that value:
def LetterChanges(str)
str.chars.map(&:next).join
end
and then you can just do:
puts LetterChanges("hello")
# => "ifmmp"