Ask A Question

Notifications

You’re not receiving notifications from this thread.

Base62 Encoding Short Codes Discussion

Hi Chris,
why the local variable result is treated as frozen?
Here is the error:
Error:
ShortCodeTest#test_encode_10:
FrozenError: can't modify frozen String: ""
app/models/short_code.rb:20:in prepend'
app/models/short_code.rb:20:in
encode'
test/models/short_code_test.rb:15:in `block in class:ShortCodeTest'

Reply

Sounds like you have a bug somewhere. Have you compared your code to the repository?

Reply

the code is the same. If I initialize the local variable result with:
result =+"" or result = String.new
no errors, tests passing

Reply

In my case works as expected, so compare again as suggested by Chris or maybe check ruby or rails version

Reply

ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".freeze

Reply

Chris,
I am very curious upon your decision to prepend the characters to the result string, instead of appending?

Reply

This version is a bit shorter.
while number > 0
number, remainder = number.divmod(BASE)
result.prepend(CHARACTERS[remainder])
end

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,796+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.