Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to require active_support in gem

I have a ruby gem and I want to use the Hash.from_xml method in the gem that is included in rails active_support module. I have the below code in my gemspec:

gem.add_dependency 'active_support', '~> 3.0.0'

However, when I build and install the gem locally, run irb, require the gem, I am not seeing the methods from active support included?

Any suggestions on what I am doing wrong or how to debug? Thanks!

like image 691
BC00 Avatar asked Sep 11 '13 03:09

BC00


People also ask

How do I require a Ruby Gem?

Requiring code RubyGems modifies your Ruby load path, which controls how your Ruby code is found by the require statement. When you require a gem, really you're just placing that gem's lib directory onto your $LOAD_PATH .

What is Activesupport gem?

A toolkit of support libraries and Ruby core extensions extracted from the Rails framework. Rich support for multibyte strings, internationalization, time zones, and testing.

How does gem install work?

What does gem install do? gem install , in its simplest form, does something kind of like this. It grabs the gem and puts its files into a special directory on your system. You can see where gem install will install your gems if you run gem environment (look for the INSTALLATION DIRECTORY: line):


1 Answers

You need to require the methods you need from ActiveSupport; they aren't added by default.

As Yevgeniy mentioned in a comment, the way to do this is require 'active_support/all' if you need everything - or if, for example, you want only the Hash extensions then use require 'active_support/core_ext/hash'. Note that this usually doesn't go in the gemspec, but rather in whatever file your gem uses to set itself up.

Perhaps even better would be to require the required ActiveSupport files in the actual files needing them, but that's a matter of taste.

like image 196
Jakob S Avatar answered Sep 22 '22 06:09

Jakob S