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
Related
I'm writing a gem that I want to work across multiple Ruby versions, what's the best way to do this? The naive solution is to do stuff like this
if RUBY_VERSION <= 1.8.7
my_hash = {:a => 1}
elsif RUBY_VERSION >= 1.9.3
my_hash = {a: 1}
...
end
What's the best way to make your gem support multiple Ruby versions?
Ruby > 1.9.3 still supports the old hash syntax. If you need to support 1.8.7 and your only problem are hash literals, the elegant solution is to use the old syntax exclusively. This way you can drop any conditionals.
You can write two versions of gems on different files within the lib directory, and on the main file, load either of them depending on the Ruby version.
Main file (foo_gem/lib/foo.rb)
if RUBY_VERSION <= 1.8.7
require_relative "./foo-ruby1.8.7"
elsif RUBY_VERSION >= 1.9.3
require_relative "./foo-ruby1.9.3"
end
When I require 'nokogiri' in Ruby 2.0, it has a error
`require': cannot load such file -- nokogiri/2.0/nokogiri (LoadError)
Is nokogiri not supporting Ruby 2.0 yet? I can see nokogiri in gem list
Ruby 2.0 support is not yet available for Windows. Follow along here for updates:
Yes, it works fine:
RUBY_VERSION # => "2.0.0"
require 'nokogiri'
doc = Nokogiri::HTML('<html><body><p>foo</p></body></html>')
doc.at('p').text # => "foo"
Nokogiri now support Ruby 2.0, even on Windows, see HERE
Here is the .rb program:
require 'watir'
b = Watir::Browser.new
the 2nd line will trigger a ""The program can't start because msvcrt-ruby18.dll
is missing from your computer!" error.
I am using 1.9.1p378 on win32
ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]
How can I fix this? Thanks for your attention.
I think the problem is that one of the win32 gems that Watir is using hasn't been updated to 1.9.1. We've fixed all the 1.9.1 issues we're aware of with the core Watir code.
Bret
I had to patch Ruby 1.9.1 with these two gems:
win32-api-1.4.0-x86-mswin32-60.gem
win32-open3-0.2.9-x86-mswin32-60.gem
Try doing a Google search for either of those gems and you should find a link. I'd host them myself but I can't access my ftp from here.
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'