Ruby - Invalid Regex - ruby

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.

Related

Encoding options not supported in 1.8: r:UTF-8

I am trying to use Ruby Uglifier gem, but it's giving this warning and the output javascript is not minified:
/usr/lib/ruby/gems/1.8/gems/uglifier-1.2.7/lib/uglifier.rb:51: warning: encoding options not supported in 1.8: r:UTF-8
The easiest answer for this is you need to upgrade to ruby 1.9. 1.8 does not support specifying a character encoding.
Tl;dr
The person who made this gem did not properly specify the supported Ruby versions in their gemspec when they built the gem, so it allowed you to install it, but it will be non-functioning due to the hard coded UTF-8 encoding. You could possibly edit the gem to remove this, but there is no guarantee that it would work, as they may be using UTF-8 specific characters in the gem somewhere. I highly recommend upgrading to 1.9.

Does :sym.eql?("sym") misbehave in IronRuby?

I just installed Mono 2.10 on my Mac and proceeded to run my Ruby Koans which I had previously finished to see if IronRuby got the same results as Mac's native Ruby 1.8.7. One of the tests midway through failed, and the issue seems to be this:
In Ruby 1.8.7, the following expression is false:
:sym.eql?("sym")
=> false
However, in IronRuby, it is true. The version of IronRuby I'm running is 1.1.2.0, which apparently is comparable to Ruby 1.9.2, so I downloaded that to check - it gets the same behaviour as Ruby 1.8.7. Is this an issue with IronRuby's implementation?
This is a bug according to the Ruby Spec. The semantics of #eql? are that it is true if == is true and if the objects are of the same class.

What's the idiomatic way of getting the version of the Ruby interpreter?

In a Ruby script, what's the idiomatic way of getting the interpreter's version as of Ruby 1.9?
You can use RUBY_VERSION constant to get the version number

Ruby 1.9.2 is backward compatible with 1.8.7?

I'm using Ruby 1.9.2 (Yarv).
When I install gems I just use "gem install ".
How can I know which Ruby version they are written in: 1.8.7, 1.9.1 or 1.9.2?
If I'm using the latest version and I install a gem that is written in 1.8.7, then that gem doesn't work?
Eg. Yardstick gem seems to not work with 1.9.2 but perfectly with 1.8.7.
How do you check what version each gem is using?
Could someone enlighten this topic for me.
Unfortunatelly there is no definitive way to check that as there are so many versions and variants or Ruby interpreters (Ruby, JRuby, Iron Ruby) and system platforms. Also one gem which is pure Ruby may not work on all platforms with same interpereter verson, because some OS specific properties could be used.
So you must trust the autor or docs he/she provided. Another way to make sure that your platform is right for gem just run tests enclosed to gem - every well written gem should have such.
There is also small tip: if file contains
# -*- encoding: utf-8 -*-
then autor probably developed the gem with 1.9.x in mind.
There's a website for answering the question: "Is it Ruby 1.9?"
http://isitruby19.com/
It doesn't look like anyone has reported on Yardstick yet:
http://isitruby19.com/yardstick
You could be the first, and report that it's broken in 1.9.

Why does 6.times.map work in ruby 1.8.7 but not 1.8.6

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'

Resources