Ruby: just-in-time gem installation? - ruby

Would it be possible to override the default "require" by adding automatic download-and-install code for any missing includes (provided that the missing include is published as a ruby gem).
Which would not work in situations where Ruby is not interfaced with a shell. But still I think it would be an interesting idea.
Is there such a mechanism in existence today?
Edit:Removed portion about password check. I just checked and gem install doesn't seem to require me to type my password.

You would be able to hijack require method so as gems are installed when an attempt is made to require them, but still you won't have access to newly installed gem in current process, because gem index has to be reloaded.
I understand the intentions but I think exercise might not be worth it.

When installing a fresh gem the gem will be installed in the GEM_HOME. If that is not writable then it will try in the user's home .gem directory (on *NIX at least).
You could certainly script this. In a way Rail's rake gems:build is just this. Just not on demand.
But, I would recommend against this. You could run into build, versioning, dependency and network issues. And probably security issues as well.
PS: Francis Hwang did something related a while ago, although only as a require, not a require gems.
http://fhwang.net/2005/11/01/urirequire-I-got-yer-Web-2-0-right-here

A better option would be to use bundler and distribute the required gems with the application.
It is also quite simple to write a script to bootstrap the installation of gems if you didn't
want to distribute them with your code (using the bundle install/check commands)

Related

What are the practical advantages of using `add_development_dependency` in .gemspec?

..vs just listing gems used for the gem development in Gemfile (while everyone uses bundler anyway)..
..except for giving your fingers extra work?
It's not clear there is any.
In theory, RubyGems has the information necessary to run tests with add_development_dependency and test_files (see this question).
Some believe that this should be outside the scope of RubyGems (see this huge thread).
In practice, there is currently no real advantage, and the fact that RubyGems still installs test files by default is a drawback, as might be the lack of flexibility that Gemfile offers.
One benefit of add_development_dependency is that if you publish your Gem to rubygems.org, those dependencies can be listed on the gem's details page. Thus if anyone finds your gem via rubygems.org, they have an idea right away what will be required if they want to contribute to the gem.

How do I ensure that gems are installed with permissions allowing all users to access them

Whenever I install a gem I can only seem to do so using sudo. This typically leaves the permissions in a state that prevents me from being able to write scripts as a regular user which don't require sudo.
Do I need to fix permissions somewhere else to allow for the installation of gems for system-wide user access?
If you are using a tool to help you bundle gems, that information might be nice. However, the gem command will let you do this, with the help of the gemspec that you should create. Looking at The Documentation you can see the executables directive. Yehuda Katz has a very good writeup on this, even though it is going on 4 years old.

Easy way to distribute ruby script

I have a bunch of ruby scripts in a folder which is added to $PATH and I think that some of them might be usefult to others. So I want to distribute them and the only 'good' way I know is rubygems (gem containing only binary), it has a very useful advantage of versioning, but also a drawback of initialization time (sometimes it takes some seconds before script starts to run). Are there alternatives?
Gem is good enought for this. I use gem for this purposes as it is very convenient to intall and update.
Gems are built for this. I'm not sure what you think a gem is, but RubyGems is a repository like PEAR for PHP, aptitude for ubuntu, or CPAN for perl, except they contain ruby libraries.
There is no extra overhead or "initialization time" added to your ruby libraries by making them gems. RubyGems simply installs your library - it doesn't do anything else.
Gems are fine for this kind of Ruby script.
To quickly generate a new gem, try out bundle gem.
To quickly distribute gems without using rubygems.org, and in a way that could work for private deployment, check out the idea for microgems.
If you still don't think you need to wrap these in gems, you can simply add the executable bit to your scripts, add shebang lines for ruby, and remove the .rb extension. Then share your script files with whoever wants them.

Where are my ruby gems?

Linux newbie question I guess..
How can I find out where gem installs the gems on my Ubuntu 10 system? I want to read gem sourcecode and perhaps change things up a bit.
Running gem environment should give you a fair bit of information about your RubyGems installation. The field you're looking for is 'INSTALLATION DIRECTORY'.
That said, if you want to modify some gems, there may be better ways to go about it. Most gems, for example, are hosted on github.com. If you make a fork there and make any improvements, you can submit them back to the maintainer in the form of pull requests. Most developers are happy to accept patches (that's why they're on github, right?), and you get a warm fuzzy for making the world a slightly better place for everybody else using that gem.
From your application directory you can run bundle show rails to reveal the path of a specific gem.
Jump to the directory like this
cd `bundle show rails`
Quickly open a gem in Sublime
subl `bundle show rails`

How to distribute a Ruby application with the required gems

I've developed a Ruby application (a small game), and I would like to 'distribute' it to other people.
However, I am not sure what to do about the required gems. If I just send my application to someone who have ruby installed, but not the required gems, I assume it will blow up. Can I package the gems locally? If so, would it conflict if the other person has a different version of the gem?
So, what is the smart/proper/good way of doing this?
The best way would probably be to just package your game as a gem as well, that way rubygems will take care of installing the dependencies. Here's the documentation explaining how to create your own gems.
If you'd rather not package your game as a gem, you could investigate the Bundler, which will be integrated into Rails 3.
In your environment.rb you can express your gem dependencies, eg.
config.gem "activemerchant", :lib => "active_merchant", :version => "1.4.1"
This isn't as automatic as gem dependencies, but it certainly usable. User must sudo rake gems:install to get your app to start.
If you're looking for a way to create OSX .dmg's and Windows Installers, there's a gem called releasy that will do all of that for you, and it is specifically tailored for releasing GUI apps written in Ruby. It bundles up Ruby and all your gem dependencies in to a single executable so that your end user doesn't have to install anything extra.
You will need access to a Windows/OSX environment to make the installers.

Resources