[MacRuby-devel] Using Gems in MacRuby

Conrad Taylor conradwt at gmail.com
Fri Oct 16 02:29:50 PDT 2009

  On Fri, Oct 16, 2009 at 12:16 AM, B. Ohr <jazzbox at 7zz.de> wrote:

> Am 16.10.2009 um 04:55 schrieb s.ross:
> On Oct 15, 2009, at 6:10 PM, Conrad Taylor wrote:
> On Thu, Oct 15, 2009 at 5:03 PM, s.ross <cwdinfo at gmail.com> wrote:
>> On Oct 15, 2009, at 1:43 PM, Laurent Sansonetti wrote:
>>  Hi Craig,
>>> On Oct 14, 2009, at 7:07 PM, Craig Williams wrote:
>>>  Hi Everyone,
>>>> I have searched the web but have not found a good explanation on how to
>>>> use
>>>> gems in a MacRuby project. Is there a tutorial I am missing?
>>> We should definitely write a tutorial about that. The RubyGems support is
>>> pretty new so I don't think anybody tried yet to embed gems in a MacRuby
>>> app. At a glance I believe it would be a matter of installing the gem inside
>>> MacRuby.framework, embed it in the app then change the GEM_HOME environment
>>> variable.
>>> Laurent
>> Oof. Changing the framework? Maybe I'm not understanding what you're
>> suggesting. Why would it not be enough to install all gems in a vendor/
>> directory and change GEM_HOME to there? Or something that would not involve
>> embedding (eventually) all gems in use in all apps you're developing inside
>> the MacRuby.framework...
>> Steve
> Steve, you should be able to install the gem(s) and require them in the
> relevant file(s).  You shouldn't have to unpack the gem(s) into a vendor
> directory because they should be visible to the Ruby environment after you
> require it.
> -Conrad
> Conrad--
> I guess I'm thinking of a case where you didn't want to rely on a
> particular gem being present on a target machine -- say for an app you were
> distributing. Naturally, the GEM_HOME built into MacRuby is just fine on
> *my* machine. But, if I give the app to someone else who may not have a
> particular gem installed, boom!
> Steve
> Steve,
> another solution would be, that somebody writes a gem-helper, which loads
> the required gems interactively into macgem when they are missing. This
> helper should have a Cocoa-UI for the unexperienced user outside and is
> called at program startup. Upgrading gems and testing a required version are
> possible features of such a helper.
> Bernd
Hi, you can actually find something like this in Rails as a rake task, 'rake
gems:install'.  Thus, you can take a look at the boot.rb within the config
directory.  Now, one can simply adapt the environment.rb file from rails for
loading gems and other components:

# Be sure to restart your server when you modify this file

# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
  # Settings in config/environments/* take precedence over those specified
  # Application configuration should go into files in config/initializers
  # -- all .rb files in that directory are automatically loaded.

  # Add additional load paths for your own custom dirs
  # config.load_paths += %W( #{RAILS_ROOT}/extras )

  # Specify gems that this application depends on and have them installed
with rake gems:install
  config.gem "hpricot", :version => '0.6', :source => "
  config.gem "sqlite3-ruby", :lib => "sqlite3"
  config.gem "aws-s3", :lib => "aws/s3"

  # Only load the plugins named here, in the order given (default is
  # :all can be used as a placeholder for all plugins not explicitly named
  # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

  # Skip frameworks you're not going to use. To use Rails without a
  # you must remove the Active Record framework.
  # config.frameworks -= [ :active_record, :active_resource, :action_mailer

  # Activate observers that should always be running
  # config.active_record.observers = :cacher, :garbage_collector,

  # Set Time.zone default to the specified zone and make Active Record
auto-convert to this zone.
  # Run "rake -D time" for a list of tasks for finding time zone names.
  config.time_zone = 'UTC'

  # The default locale is :en and all translations from
config/locales/*.rb,yml are auto loaded.
  # config.i18n.load_path += Dir[Rails.root.join('my', 'locales',
  # config.i18n.default_locale = :de


Now, you simply need to execute the following command to install the missing
components before starting the app:

rake gems:install

Otherwise, you can create an application subdirectory called lib which is
added to your load path when your application
is started.  For example,

library_paths = []
library_paths << "#{FILE.dirname(__FILE__)}/lib/some_library_one"
library_paths << "#{FILE.dirname(__FILE__)}/lib/some_library_two"
library_paths << "#{FILE.dirname(__FILE__)}/lib/some_library_three"

library_paths.each do | library_path |
  $:.unshift( library_path ) if File.directory?( library_path )

Now, you can throw all this into some type of dependencies.rb file that gets
loaded before application starts.  This may be a better approach to
follow for end users instead of expecting them to execute a command before
application starts.  Thus, you can adapt the above library_path
example to install gems as well if your application depends on them.
 Finally, you may want to also look at the Rubygems API because it does
all this stuff and more for managing gems.

Good luck,


> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20091016/639072c8/attachment.html>

More information about the MacRuby-devel mailing list