Converting text to unit length (string.to_l)
-
Originally posted on the Smustard forum:
Be careful when using string.to_l to convert text to unit length. Windows users in countries that use "," for decimal separation will likely have regional settings active for their OS, and Ruby is dependent on those regional settings. For the US, "23.563m".to_l will work fine, but in those locales, it will fail. Conversely, "23,563m".to_l will work on their machines, but will fail where "." is used for decimal separation.
A simple begin/rescue/end can set a class or instance variable for handling the proper notation convention. I may try to code this into a separate class.
-
Yes, I was worried about this problem. In my woodwrk.rb I use a simple math evaluator as an alternate to simply a math expression. I parse the string and where there are legitimate length fields I insert the following: s.to_l.to_f.to_s.
If fear this won't work in European countries although I do know at least one guy using my Ruby.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register LoginAdvertisement