Is there a way to use Gems in Vim's 'embedded' Ruby? - ruby

I'm trying to use the tinder gem from inside Vim. I'd like to be able to write to the Vim buffers, so I need to use Vim's embedded Ruby using :ruby as opposed to externally calling !ruby.
I have run gem install tinder with no problems on the command line, but embedded ruby doesn't seem to have the relevant directories on its load path to be able to require it.
I've tried manipulating the load path by trying things like:
:ruby `gem env gempath`.strip.split(':').each { |p| $:.unshift(p) }
... but with little success.
I've also tried a similar thing with:
Gem.path.unshift ...
... but, again, with little success.
I've tried unpacking Tinder and requiring an absolute path, which does seem to work, but unpack doesn't unpack the gem's dependencies, so it cannot find 'faraday', for example. Perhaps I could recursively unpack?
Does anyone have any thoughts on this issue?
I've googled around a lot and looked at the source of projects like Vmail, but as far as I can tell, no one is using Gems within Vim's Ruby. This seems an awful shame.
I'm pretty sure gem native extensions will never work, whatever I try- but I'd be very happy just being able to require pure Ruby gems.
Many thanks.

After hunting around for a long time, it's actually pretty simple.
The easiest way is to compile Vim against a version of Ruby that's 1.9 or greater. Vim will use whichever Ruby is first in your load path when you compile.
Then you just need to install gems through the conventional means for the version you compiled with.
The gems will be available in your load path by default in 1.9 and onwards because they made some changes to the way rubygems gets autoloaded.

Related

How do I get rrdtool from homebrew to work with ruby on macOS

In our Rails application we do require 'RRD' at some point, but that results in a cannot load such file -- RRD. So obviously I used homebrew to install rrdtool, but the error remains.
The docs at https://oss.oetiker.ch/rrdtool/prog/rrdruby.en.html provide two options:
Either:
$: << '/path/to/rrdtool/lib/ruby/1.8/i386-linux'
require "RRD"
In my /opt/homebrew/Cellar/rrdtool/1.8.0/lib directory there's no mention of ruby, which is because of the --disable-ruby-site-install flag in the formula, because when I skip that flag I do actually get something: /opt/homebrew/Cellar/rrdtool/1.8.0/lib/ruby/2.6.0/universal-darwin21. However replacing the path/to string with this path still gives the error.
Or:
If you use the --ruby-site-install configure option you can drop the $: line since the RRDtool module will be found automatically.
Which is a little confusing (and probably outdated) because here it seems that ruby site install is disabled by default and you have to enable it proactively, whereas in the formula it's actually actively disabled.
Either way: both options didn't do the trick for me and if there's a solution without homebrew that's also fine.
For good measure: I'm on macOS Monterey
TL;DR
For the most part, I'd say that using a non-standard gem without a Ruby version manager is your main issue. There are instructions on the rrdruby site for installing it, but they don't follow typical conventions, so your mileage will vary.
Some Practical Suggestions
The require keyword is for gems, not binaries. You need to have an rrdtool-related gem installed, available to your Ruby instance (usually through a Bundler Gemfile or gemspec, or via the RUBYOPTS environment variable or your in-process Ruby $LOAD_PATH), and then require the correct name of the gem in your code. For example, using the older rrd-ffi gem:
# use sudo if you're installing it to the system,
# but I would strongly recommend a ruby version
# manager instead
gem install rrd-ffi
# in your Ruby class/module file
require "rrd"
For the gem you seem to be using, you have to compile the gem first to make it usable, and then ensure it's available in your Ruby $LOAD_PATH (or other gem lookup mechanism) before trying to require it. The error message you're seeing is basically telling you that a gem with that name is not available as called within any of the standard lookup locations.
Again, I'd suggest reading the build documentation for your gem, and then seeing if you can install it as part of a Bundler bundle, RVM gemset, or other non-system approach if you can. Otherwise, follow the directions for the rrdruby tool, which is not available as a standard Rubygems.org gem, in order to make it available before trying to require it.
Beware of Outdated or Non-Standard Gems
Most of the RRD gems I found were quite old; most were 7-8 years old or older, so their compatibility with current Rubies is potentially suspect. The gem-builder you're using is newer, but doesn't seem to be designed as a standard gem, so you need to build it and install it in a suitable lookup path before it can be required. Installing gems as system gems is almost always a bad idea, so I'd strongly recommend building it from source and using a ruby version manager rather than following the rrdtool author's atypical suggestions. YMMV.

Change gem env RUBY EXECUTABLE path for one command

I would like to run gem commands, such as gem install, with a different ruby version than what is listed in gem env. The Ruby version I want to use is a pre-compiled version which I have the path for, so installing and using another version from RVM or similar would not solve my problem.
I do not want to change the RUBY EXECUTABLE permanently, just for one command at a time. I have tried to set GEM_HOME, GEM_PATH, PATH, RUBY and more. I have tried firing up gem with specific/version/of/ruby/path/ruby path/to/gem env, but I still get the default Ruby in my RUBY EXECUTABLE variable.
I even tried settingRUBY_EXECUTABLE=/path/to/correct/ruby, which also did not work.
What really surprised me was that when I edited the shebang in the path/to/gem file itself so it pointed to the correct Ruby, it still did not work! What is up with that?!
How can I change this variable so I can use gem goodness with my custom compiled Ruby?
This one is really beating me. I have now updated my rbconfig.rb to point to the desired Ruby path. I have looked at the rubygems source and replaced every single instance of the default ruby , in all the files I could find, with the path to the one I want. Even this did not set the environment correctly. Is this somehow hard-coded into the compiled ruby? If that is the case, why the star*4 is this done?
Try using rbenv (https://github.com/sstephenson/rbenv) or RVM to manage Ruby versions (https://rvm.io/). When you switch Ruby versions with rbenv, gem env will use use the new Ruby version. The following command can be used to change the Ruby version for a single shell:
$ rbenv shell 2.1.2
After hours and hours of research, stepping through the Ruby source with Pry, reading source code and more I figured out that this is not possible to do because it is hard-coded into ruby at compile time (wtf?). Anyway, the way to solve this is to simply recompile Ruby. Yeah.
There is also apparently a compile flag which you can set which removes this hard-coded environment: --enable-load-relative
After struggling with this for way to long I finally got this project working, where I have made an easy to use portable version of Ruby. Simply put, a folder with Ruby on it which you can move about, put on a USB stick or whatever, and it still works :)

How do I easily condense a whole ruby program into one file?

I really like writing command line applications in ruby. However, when working on lots of ruby projects I end up switching between different ruby versions with something like rvm, rbenv or chruby. This means I need to install ruby command line utilities for every version of ruby I work on. I've gotten used to this, but when distributing ruby command line apps to other devs they get confused when running whateverrubyapp that worked in one directory now doesn't work in another directory because the ruby version is different, or even worse they're using gemsets.
Is there an easy way to bundle up a ruby application with all it's dependencies including gems into a single file so that I can just have people put that file in their ~/bin dir so that it behaved somewhat as though it were a compiled application?
For example, I whipped up a little rake task to take a project with no gem dependencies and put it all into one file https://github.com/mmrobins/git-rank/blob/master/Rakefile. However, I have much more complex command line apps that have gem dependencies I would want to include into a single file for easy distribution.

Creating a gem with Bundler

I'm trying to create a gem with Bundler, following this guide: http://rakeroutes.com/blog/lets-write-a-gem-part-one/. In it, it says:
I incorrectly thought after taking my first look through the gemspec
that I would need to add more require statements as I developed my
gem. That isn’t the case: the files just need to be in git.
I am trying to clean up some of my old gems to use this convention, but when I install my gem, the classes from the other files are not available. I have a couple directories nested under my /lib dir, but I wouldnt think that would be an issue. Is there anything simple to overlook that would prevent my other files from being required? Any help would be appreciated.
In the link, when he says he doesn't need to add a lot of "require" statements, he must mean adding files to the s.files, s.executables, and s.test_files arrays--these determine what files get packaged up into the gem and what files get ignored. As you can see from the gem spec, whatever's tracked by git in certain directories is going to be included in the packaged gem.
Ruby's require is a different story. Standard require rules still apply.
Ruby's gem system works by adding a bunch of different places for Ruby to look for "foo.rb" when you run require "foo". If "lib" is your only require path for your gem, when you require "my_gem" Ruby is only going to run the code in lib/my_gem.rb. If lib/my_gem.rb doesn't require any other files in your gem, then Ruby hasn't seen them and so you'll get undefined constant errors when you try to use the classes from those files.
For examples, you might take a look at two simple gems I've written; both were started with bundle gem: HashToHiddenFields and SimpleStats. In both gems, main Ruby file in lib/ requires everything that needs to be loaded for the gem to work correctly. For example, hash_to_hidden_fields.rb requires action_view/helpers/hash_to_hidden_fields so that the ActionView::Helpers::HashToHiddenFields constant+module exists so we can include it into ActionView::Base.
Hope that answers the question. I know Ruby requiring was pretty fuzzy to me for a while.

Getting started with gems and jeweler

With Jeweler I created a gem folder structure with ease.
However, I still have some questions:
Why are params like --gemcutter and --rubyforge still available for Jeweler. Aren't these replaced by RubyGems? Do I have to specify anything to create a gem for RubyGems?
In the Rakefile I have information about the gem, and when I run "rake install" it created a gemspec. Why is the same information in two places?
What is a manifest? Just read about it, haven't seen such file.
How do I make my gem callable from the shell once I have installed it, like rails. Cause right now it's just accessible through a Ruby script using require.
Should I use "jeweler release" or "gem push" to push my gem to RubyGems.org?
I have to specify "handle" when signing up in RubyGems. What is that?
Thanks.
jeweler was created before RubyGems became what it is, so it still reflects the split. I'm not sure when jeweler was last updated, either. (I think it also still recognizes building gems on Github, which is now disabled.)
I'm not sure I follow what you're saying. The specification in the Rakefile details what the spec that gets written should look like. The spec that gets written details what should be installed and how, I believe.
A manifest is a list of all the files that your gem should ship with. Not everyone uses one. See the hoe documentation for some pro-manifest discussion.
Many Ruby gems are only libraries. If you want yours to also have a program like jeweler or rake or rails that you can call, you have to write the callable program, put it in bin in your gem's layout and specify (in your gemspec) that it should be packaged and installed. See the Gem::Specification reference under files and executable.
Not sure. Consult both jeweler's docs and the docs for RubyGems.
You can give an email address or use a name (a 'handle', like I use Telemachus here), which is all they mean by 'handle'.
For the record, if you are just learning how to write gems, you do not need to upload your first attempts using RubyGems or anything like it. You can simply install the gem on your machine only.

Resources