can't load gem in macruby - ruby

I just started using MacRuby for a simple Mac Application, and I now have a problem.
For my project I use the rmagick gem.
I installed it as described with the command
sudo macgem install rmagick
Everything worked well, but the problem is that if I try to load it in my AppDelegate.rb file with require 'rmagick'. I get an error saying that there is no such file to load.
This is strange, because I noticed that if I first build the project normally and then build the deployment target, I'm able to execute the application without any problems.
It would be really cool if there would be a way to use the gem in the development target, because otherwise I need 30 seconds after each change I made which in total is really much xD

Yeah RMagick can be a tricky one:
require 'rubygems'
require 'RMagick'
is the answer. Also macruby apparently still requires the require 'rubygems'. But beware, the module you actually will be using is called Magick.
A short example:
require 'rubygems'
require 'RMagick'
image = Magick::Image.new "path/to/image.png"
Here you will find the RMagick-documentation.

Related

Requiring scripts is broken when using RVM

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

Generating RDOCs for locally installed gems

I am trying to contribute to a gem I recently took interest in - Nesta. The developer has done a great job in creating one of the lightest, thinest CMSs you can find and I want to document it. I have read through the code and commented on a few methods to the best of my knowledge.
However, I seek to test this out locally by calling gem server and seeing the changes on my machine before pushing it online.
Things I have tried:
Manual edit.
Documented the file.
Fired up gem server.
Using the gem tool.
Documented the file.
Ran gem rdoc nesta --rdoc
Restarted gem server
All to no avail. Please help.
Thank you.
You can preview generated html pages without installing a modified version of the gem on your machine. Add this to nesta's Rakefile:
require 'rake/rdoctask'
Rake::RDocTask.new('doc') do |i|
i.rdoc_files = FileList['lib/**/*']
end
and type rake doc. Then view generated html/index.html file.
Are you sure you've installed the version that you've modified, not the original version?
If you've installed the modified version, but have forgotten to install the rdoc, see Can you install documentation for existing gems?

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.

gem command . What does that mean

Sometimes I have seen following code.
gem 'factory_girl','= 1.2.3'
require 'factory_girl'
I tried to look at gem doc but could not find answer to the question of what does the first line do in code above?
What you're looking for in the gem docs is about Coding with Rubygems.
The first line basically says "Hey, go get this gem with this version" from the install directory for gems and load it into the environment. This is mainly to help you add version dependencies to your requires instead of just doing require 'factory_girl' by itself.
Edit: To add on to Jörg's point below, I thought that Ryan Tomayko had a pretty good short and sweet article about why doing this is "wrong".
As #theIV already explained, this activates the factory_girl gem, using exactly (because of the = sign) version 1.2.3.
Note, however, that this is very bad practice and should never be done. If you activate gems manually inside your code, it means that people who do not use RubyGems can no longer use your code.
RubyGems is a package manager. Your code should never care about what package manager was used to install it. Some people prefer RubyGems, some dpkg/APT, some RPM/YUM, some RPM/APT, some RPM/URPMI, some RPM/YaST2, Portage, FreeBSD ports, pkgsrc, MacPorts, slashpackage, CoAPP, Conary, Slackware. There's tons of them. Some people like not to use any package manager at all. Or, they use RubyGems just for downloading, but then unpack the gem into their vendor directory.
All of this cannot possibly work, if you use the gem method in your code.

Can gems be used by ruby code compiled with macrubyc

MacRuby 0.5 includes a ruby compiler built on LLVM called macrubyc.
Does anyone know if it would be possible to dynamically load gems from compiled code? Or compile the gems and link them in? Is this planned? Or how compiled code will be able to make use of gems in general.
Turns out that as of MacRuby 0.6, something like this will compile
require 'rubygems'
require 'sequel'
DB = Sequel.mysql(...)
But fail at run time trying to load mysql leading me to think that it loads the gems at run time.

Resources