Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'English' global variables don't work in Rails

I'm trying to use the English gem in Rails, so I can access $LAST_MATCH_INFO, the less cryptic version of $~. However, despite requiring English, $LAST_MATCH_INFO is always nil, while $~ still works.

This is my expected behaviour from irb:

'foo' =~ /o/           # => 1
$~                     # => #<MatchData "o">
$LAST_MATCH_INFO       # => nil

require 'english'      # => true
$LAST_MATCH_INFO       # => #<MatchData "o">
$LAST_MATCH_INFO == $~ # => true

This is the broken behaviour from rails console:

'foo' =~ /o/           # => 1
$~                     # => #<MatchData "o">
$LAST_MATCH_INFO       # => nil

require 'english'      # => false
                       # (Means it's already been required)
$LAST_MATCH_INFO       # => nil
$LAST_MATCH_INFO == $~ # => false

It works in irb, but not in rails console, or in the web console. Notice that require 'english' returns false because the gem is already loaded, and yet it doesn't work.

What am I missing?

like image 770
bosticko Avatar asked Dec 21 '25 13:12

bosticko


1 Answers

TL;DR: Removed the gem 'english' from Gemfile, works.

I ended up coming back to this months later and figuring it out.

In my hastiness to use the English library, I added the english gem to my Gemfile. (Notice the capitalization).

While English is the built in library I wanted, for descriptive global variables, english is a gem for general english language processing.

Normally, require 'english' would require the gem, and require 'English' would require the library. However, I am using a case insensitive Mac OS X system, so require 'English' found the english gem first and used that. This meant it was impossible for Ruby to load the English library, which is why it never worked.

like image 108
bosticko Avatar answered Dec 24 '25 01:12

bosticko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!