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.
Advertisement