I am using Ruby 1.8. It seems that downcase does not alter non-latin characters. For example:
"Δ".downcase
returns "Δ"
I know that in Ruby 1.9.1 and later, I can use Unicode Utils (from here). I have tried it and it works ok. Returns "δ" for the previous example.
Is there an equivalent (or any) solution for 1.8 Ruby?
nash#nash:~$ ruby -v
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-linux]
gem install unicode (https://rubygems.org/gems/unicode)
require 'unicode'
$KCODE = 'u'
p Unicode::downcase "Δ" #=> "δ"
Related
Ruby 1.8.7 says:
/Users/user/.thor/916bf40c98406efffe9700e1ef02da24:194: undefined (?...) sequence: /(?<text>Version:)\s*(?<version>[\d\.]+)/
Ruby 1.9.3-p374 says something similar, but Ruby 1.9.3-p0 works just fine with it.
The actual code that's broken:
pattern = /(?<text>Version:)\s*(?<version>[\d\.]+)/
What am I doing wrong ?
The (?<k>...) notation expresses named capture, which was introduced in Ruby 1.9, which uses Oniguruma regex engine. With Ruby 1.8, you need to either change the regex to not use it, or install Oniguruma or Onigmo (an improved version of Oniguruma introduced in Ruby 2.0).
Your code works fine for me in Ruby 1.9.3-p374. I suspect that what you are doing wrong is still running 1.8.7 when you think you're using 1.9.3-p374. As noted by #sawa, your regex is using a feature introduced in 1.9.
I used the rubyzip gem in Ruby 1.8.7 before, but I heard rubyzip doesn't work well with ruby 1.9.2.
What zip libraries work well with Ruby 1.9.2?
Have you actually tried using rubyzip with 1.9.2? Seems to work fine for me:
>> RUBY_VERSION
#=> "1.9.2"
>> require 'zip/zip'
#=> true
>> Zip::ZipFile.foreach(File.expand_path("~/Downloads/Archive.zip")) { |f| p f }
#=> [bartxt, footxt]
bar.txt
foo.txt
I used rubyzip gem in Ruby 1.8.7 also. For Ruby 1.9.x you need to use version 0.9.5 or higher. Works without any problems.
I found zip it says it's compatible with 1.9.1 I don't think it would have any issues in 1.9.2
According to the Ruby docs for Time#zone:
As of Ruby 1.8, returns "UTC" rather than "GMT" for UTC times.
My OSX-using comrades see this behavior.
On my Ubuntu 9.10 system, however, Ruby 1.8.7 seems to prefer the old "GMT" terminology:
$ ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
$ ruby -e 'puts Time.now.utc.strftime("%Z")'
GMT
Anyone have any idea why?
I believe this is a bug/feature in .strftime, which more or less uses the underlying C strftime call. If you display the timezone in other ways (e.g. "puts Time.now.utc") you see "UTC".
I am trying to use the Enumerable#each_slice. It doesn't work on my computer, stating that method is not found.
I am running ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
API: http://ruby-doc.org/core/classes/Enumerable.html#M003142
Example:
(1..10).each_slice(3) {|a| p a} # I get NoMethodError: undefined method `each_slice' for 1..10:Range
What am I doing wrong?
In ruby 1.8.6 you have to require 'enumerator' (which is part of stdlib and has been merged into core in 1.8.7+) before using each_slice.
Sadly the ruby-doc lists methods that are added to core classes by stdlib without mentioning where the methods are from.
just compared 1.8.6 to 1.9 and it looks like
(1..10).respond_to? :each_slice
is true in 1.9 and false in 1.8.6. So, the doc you are using is not for 1.8.6. if you can upgrade to a newer version of Ruby easily it should give you that method on the Range.
The following code snippet works fine in 1.8.7 on Mac OS X, but not in 1.8.6 on Ubuntu. Why? Is there a workaround?
Works in 1.8.7:
$ ruby --version
ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]
ltredgate15:eegl leem$ irb
>> 6.times.map {'foo'}
=> ["foo", "foo", "foo", "foo", "foo", "foo"]
>>
But not in 1.8.6:
# ruby --version
ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-linux]
Ruby Enterprise Edition 20090610
# irb
irb(main):001:0> 6.times.map {'foo'}
LocalJumpError: no block given
from (irb):1:in `times'
from (irb):1
irb(main):002:0>
Why is there a difference? What's the workaround for 1.8.6?
In 1.8.7+ iterator methods like times return an enumerator if they are called without a block. In 1.8.6 you have to do
require 'enumerator'
6.enum_for(:times).map {...}
Or for this specific use case you could simply do (0...6).map {...}
In Ruby 1.9, the library was changed so functions that did iteration would return an Enumerator object if they were called without a block. A whole host of other language features were also changed, and it was widely known that compatibility would be broken between Ruby 1.8.x and Ruby 1.9 in the interests of improving the language as a whole. Most people didn't find this too distressing.
The Ruby development team decided that Ruby 1.8.7 should be a transition release adding some of the library features that Ruby 1.9 introduced. They took a lot of criticism for the decision, and many enterprise Ruby users remained (and many still remain) running Rails on Ruby 1.8.6, because they feel the changes introduced 1.8.7 are just too large, and too risky. But nevertheless, 1.8.7 remains, and having iteration functions return Enumerators is one of the features that was incorporated.
It is this migration feature that you're seeing in 1.8.7, which is not present in 1.8.6.
sepp2k's answer gives a good workaround. There's not much for me to add on that count.
Because 1.8.6 #times yields on the given block, while 1.8.7 returns an Enumerator object you can keep around and implements Enumerable.
Ruby 1.8.7 introduces many changes. If you want to use them in Ruby 1.8.6, simply
require 'backports'
That's it. This gives you many methods of 1.9.1 and the upcoming 1.9.2 as well, although it's possible to require 'backports/1.8.7' for just the changes of 1.8.7, or even just the backports you need, e.g. require 'backports/1.8.7/integer/times'