1

I am not quite sure what I am missing. Thanks for the help in advance!

# Example:
# encoded_string = "m1i1s2i1s2i1p2i1"
# decoded_string = decode(encoded_string)
# decoded_string # => "mississippi"

# decode("f1o2d1")
# "food"

def decode(string)
  new_str = ""
  i = 0
  while i < string.length
    letter = string[i]
    num = string[i + 1]
    num.times {new_str += letter}
    i += 2
  end
  return new_str
end
2
  • It helps us a lot if you include the code in the body of your question. Saves us clicking around, and also some people can't load those links due to firewall rules. Commented Jan 28, 2020 at 22:07
  • You need to explain your problem, which is that a "no method error" exception is raised when the line num.times {new_str += letter} is executed. For example, decode("f1o2d1") #=> NoMethodError (undefined method 'times' for "1":String). That's because num is a string, yet the class String has no instance method times. Note that this error message pinpoints the problem. Commented Jan 28, 2020 at 22:43

2 Answers 2

1

You've defined a function, but you need to call it or nothing will happen.

As in the example:

decode("f1o2d1")

However there's a bug in your code when you do that.

Consider using string.chars to get the characters and then each_slice(2) to pull out pairs. For example:

string.chars.each_slice(2) do |char, count|
  count = count.to_i

  # ... Do stuff with this
end

Another trick in Ruby is if you want to repeat a string, multiply it:

'a' + 'b' * 2 + 'a'
# => "abba"
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! Appreciate the help
I can't believe it! You wrote, string.chars.each_slice(2) do .... Surely you mean string.each_char.each_slice(2) do ....
@CarySwoveland They both work, each_chars is more efficient. This is an introductory example, mind you, so chars is more self-explanatory.
Candice, string.chars returns an array. string.chars.each_slice(2) chains the array to the method Enumerable#each_slice, returning an enumerator. An enumerator generates objects. Here those objects are passed to a block. string.each_char.each_slice(2) does the same thing, except string.each_char returns an enumerator, avoiding the creation of the temporary array produced by string.chars, saving memory. The rule is to use chars if it is chained to an instance method of the class Array; use each_char if it is chained to a method that accepts an enumerator as its receiver...
...but being new to Ruby you don't have to be concerned with that now. Just tuck away that sometimes its best to use each_char rather than chars.
1

The issue is here:

num = string[i + 1]
num.times {new_str += letter}

string[i + 1] returns String—in this case a single character that happens to be a number, like "2". times, however, is a method that belongs to the Integer class. In order to use times on that number, you need to first convert it to an Integer using to_i:

num = string[i + 1].to_i

With that change your code works perfectly: https://repl.it/@jrunning/CriminalGracefulRefactoring

1 Comment

I suggest you also mention what happens when i equals string.length - 1. I suspect that num should be set equal to string[0] in that case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.