From: Brian Candler on 19 Apr 2010 04:03 >> Perhaps, but I was talking about an identical platform, O/S, and >> installation of ruby - but different configured locale (such as LANG, >> LC_CTYPE or LC_ALL environment variables) > > So your main complaint is that Ruby honors the settings of your > environment? My complaints are listed at http://github.com/candlerb/string19/blob/master/soapbox.rb - but I guess the main one is what the OP saw. Same program, same data, same ruby, different behaviour. Normally when analysing a program you only need to look at the program and its input, but ruby 1.9 has extra "hidden" input data in the form of environment variables which can alter your program's behaviour, or not, depending on the content of the input data as well. I wonder how many Ruby users are fully aware of which environment variables influence POSIX locales, and which ones take precendence over the others? I also note that there is an effort underway to standardise the Ruby language definition, and this has chosen 1.8.7 as its baseline. -- Posted via http://www.ruby-forum.com/.
From: Brian Candler on 19 Apr 2010 04:31 botp wrote: >> So if you want to write programs which don't crash, the first is >> arguably better. > > we disagree there but what do you mean by "crash"? I mean "raise an exception". The first example I wrote will never raise an exception. The second can. Code to demonstrate: str = "\xff" File.open("out1","w") { |f| f.puts str } File.open("out2","w:UTF-8") { |f| f.puts str } Line 2 will never raise an exception, regardless of the content or the encoding of str, and regardless of environment variable settings. It just writes the string to the file. Line 3 may raise an exception. It does in this particular program because str has data tagged as ASCII-8BIT which cannot be transcoded to UTF-8. -- Posted via http://www.ruby-forum.com/.
From: James Edward Gray II on 19 Apr 2010 11:46 On Apr 19, 2010, at 3:31 AM, Brian Candler wrote: > Code to demonstrate: > > str = "\xff" > File.open("out1","w") { |f| f.puts str } > File.open("out2","w:UTF-8") { |f| f.puts str } > > Line 2 will never raise an exception, regardless of the content or the > encoding of str, and regardless of environment variable settings. It > just writes the string to the file. That's grossly inaccurate. You may not have write permission to the file, the volume you are trying to place the file on may be out of space, etc. These are more examples of how you could move the same code to a new machine and have it fail. Ignoring the environment code runs in will not make it go away. James Edward Gray II
From: Brian Candler on 19 Apr 2010 16:28
James Edward Gray II wrote: > On Apr 19, 2010, at 3:31 AM, Brian Candler wrote: > >> Code to demonstrate: >> >> str = "\xff" >> File.open("out1","w") { |f| f.puts str } >> File.open("out2","w:UTF-8") { |f| f.puts str } >> >> Line 2 will never raise an exception, regardless of the content or the >> encoding of str, and regardless of environment variable settings. It >> just writes the string to the file. > > That's grossly inaccurate. You may not have write permission to the > file, the volume you are trying to place the file on may be out of > space, etc. Of course syscalls can fail due to insufficient resources and other system-level problems. I'm talking about the normal flow of execution. The point remains: Benoit said that one way to make your program immune to influence from environment variables was to use File.open("myfile.ext","w:UTF-8"). I was trying to highlight that advice is incorrect, because the regular File.open("myfile.ext","w") is immune to environment variables already. Furthermore, "w:UTF-8" can crash in the normal flow under more circumstances than "w" - and those circumstances depend on string contents and encodings, which _can_ be affected by environment variables. -- Posted via http://www.ruby-forum.com/. |