Requiring scripts is broken when using RVM - ruby

Been running into a problem ever since I started using rvm to manage my ruby installation. Whenever I want to require another class I've written, such as require 'filename', I get a require - no such file to load error when I try to run my script. If I switch back to my system ruby using rvm use system it works again, but I'm 1.8.2 as my system ruby and some features I want to use in my code are only available in 1.9.*, which I can access through rvm. How do I fix this problem?

I'm not sure this is a problem with RVM, but a problem with Ruby 1.9 not including '.' in the current path. For instance, if you are trying to require a library at './lib/file.rb', you can't do
require "lib/file"
You have to do this:
require "./lib/file"
Have you tried that syntax for your require?

I think you want to use require_relative
http://extensions.rubyforge.org/rdoc/classes/Kernel.html

Related

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 :)

Is there a way to use Gems in Vim's 'embedded' 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.

Use RbConfig instead of obsolete and deprecated Config

While trying to install ImageMagick on Windows through msys.bat by running
ruby setup.rb install
I'm getting this error:
setup.rb:787: use rbconfig instead of obsolete and deprecated config.
no such file or directory -.config
setup.rb config first
Try ruby setup.rb --help for detailed usage
RbConfig is a Module which gives access to mostly compile time properties of the current Ruby implementation. To use RbConfig one has to require 'rbconfig'. In earlier versions of Ruby this class was just called Config and was loaded through require 'config'. When trying to use Config nowadays, Ruby will give out a deprecation warning.
Go into your setup.rb into line 787. There you will probably find a require 'config'. Change that into a require 'rbconfig'. If that doesn't make the script run or there are still warnings, search and replace usages of the Config module with RbConfig. Then Ruby will shut up.
The problem probably only appears the software you are trying to compile is not up to date. I guess an even better way to make it work would be to find a more current version of it, which may solve additional problems.
Had same problem solved it through:
#aef's answer and ther changing the file to look like below:
require 'tempfile'
if i = ARGV.index(/\A--rbconfig=/) then
file = $'
ARGV.delete_at(i)
require file
require 'rbconfig'
else
require 'rbconfig'
end

Ruby on Windows path for requires in dir not working

I've a small ruby program that require files in the same directory. Program works perfect on my mac and when I run a test ruby script without any require it also works so. It seems the ruby program doesn't look in the current directory for the file by default. e.g. the . dir. In windows where do I need to update this so ruby does look in the current dir for requires?
Chances are that your Mac is running Ruby 1.8 and Windows is running Ruby 1.9. As of 1.9, the default load path no longer includes the current directory. A common practice is to add this to the top of your ruby file before your require statements
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'my_file.rb'
You can also use the shorthand $: instead of $LOAD_PATH:
$:.unshift File.dirname(__FILE__)
Another alternative is adding the load path on the command line instead:
ruby -I. my_ruby_file.rb
Ok, I understand now since 1.9.2 for "Security" reasons they don't allow require to work like that anymore. The neatest way I found to solve it strangely was to put './' in front of every require.
e.g.
require "./myfile.rb"
"." was removed from $: was removed from Ruby 1.9.2 to be precise. Do
puts RUBY_VERSION
puts $:.inspect
on Ruby 1.8 (what's installed on your Mac) and Ruby 1.9.2 (what's installed on your windows machine) if you don't believe me.
Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative? discusses why "." was removed.

Sinatra cannot find views on Ruby 1.9.2-p0

I'm quite new to Ruby language (up to now I developed in Groovy + Grails) but since I was curious about it I wanted to try Sinatra on Ruby 1.9.2-p0.
I have a trivial website that is contained in /mywebpage and has 2 files:
# blog.rb
get '/' do
'Hello World!'
end
get '/impossible' do
haml :index
end
and
#config.ru
path = File.expand_path "../", __FILE__
$LOAD_PATH << (File.expand_path ".") + "/views"
require 'haml'
require 'sinatra'
require "#{path}/blog"
run Sinatra::Application
then in the same folder I have a /views/ folder that contains index.haml.
I try to run the server with rackup -p8080 but when I try to get /impossible I receive the following error:
Errno::ENOENT at /impossible
No such file or directory - /home/jack/mywebpage/<internal:lib/rubygems/views/index.haml
By searching over internet it seems that this maybe caused by "." not being included in $LOAD_PATH so I tried to add it or add directly views ./views so that actually $LOAD_PATH.inspect gives me correct path: ..., "/home/jack/mywebpage/views"]
But still it doesn't seem to work. Being quite new to the framework and the language I was wondering if I'm doing something wrong. any clues?
Running Sinatra with Ruby 1.9.2 the template directory is no longer implicitly 'views', you need to set it yourself.
set :views, File.dirname(__FILE__) + "/views"
Note that currently Ruby also has Kernel#__dir__() method that is equivalent to File.dirname(__FILE__).
This, and other issues with 1.9, will be have been solved in Sinatra 1.1. You could use this fork: http://github.com/rkh/sinatra/tree/1.1
I ran into a similar problem, and solved it like this. I didn't dig into the problem, but this is what I found and it works. It'll supposedly be fixed in the next version of Sinatra (which they should really get out the door, just to fix these few 1.9.2 bugs).
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
enable :run
get '/' do
"Hello, world!"
end
Edit: It seems there are multiple bugs with Sinatra on 1.9.2. This one will fix Sinatra apps not starting on 1.9.2. I don't use a views directory (I like to keep my apps single-file), so I didn't run into your particular problem. This fix most likely won't help you at all. I probably should have read your problem more closely..
gem install sinatra --pre
I ran into that last week and didn't find a suitable fix on the Sinatra site short of tweaking the sinatra code. I'm using rvm for my development and switched to try sinatra on Ruby 1.8.7 and it works fine again, so that's where I left it.
Oh, since you're new to Ruby, you might not know about rvm, so here's the lowdown. RVM is Mac only and highly recommended for managing your Ruby version and gems. It makes it trivial to have multiple Ruby versions and alternate groups of gems for development and testing. Everything is stored in your ~/.rvm directory so it's easy to blow it all away if you need to.
http://rvm.beginrescueend.com/
I just looked at the Sinatra site again about the problem to see if there was anything new, but it appears they consider the following to be an acceptable fix:
http://github.com/sinatra/sinatra/issues/#issue/50
I'm a bit adverse to having to edit the source of Sinatra as recommended by issue #50, but it's not real hard to do. I'd like to see them put out an update so we'd have an official fix but I haven't seen anything yet:
gem env will tell you the "GEM PATHS". Sinatra's gem will be in one of those. The line mentioned in issue #50 goes into base.rb. On my machine it's something like ...gems/ruby-1.9.2-p0/gems/sinatra-1.0/lib/sinatra/base.rb.
Insert:
/<internal:/, # ruby 1.9.2-p0 hacks
at line 1020.
Save the file and you should be good to go.

Resources