How to "require" manually installed gems (KDE plasmoid)? - ruby

I'm trying to write a Ruby plasmoid for KDE. I need to use barely one rubygem. Whenever I write require 'dbus', it throw me and an error:
code/main.rb:6:in 'require': no such file to load -- dbus (LoadError)
code/main.rb:6:in '<module:TestApp>'
code/main.rb:5:in '<top (required)>'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:177:in 'load'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:177:in 'init'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:201:in 'constraintsEvent': undefined method 'constraintsEvent' for nil:NilClass (NoMethodError)
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:201:in 'constraintsEvent': undefined method 'constraintsEvent' for nil:NilClass (NoMethodError)
Actually, normal "ruby main.rb" works well (regarding on "require" part), but testing plasmoid with "plasmoidviewer" fails. Note, that regular gems from standart Ruby installation works well, i.e. require 'Qt4' or require 'yaml' loads perfectly. I'm using Ruby 1.9.2p180 under Linux.
09:40 PM - UPDATE: Richard Dale, one of the QtRuby developers, just fixed this issue a few minutes ago. Next release of KDE will have patched version of QtRuby.

require 'find'
require 'findUtils'
Find.find(PATH_WHERE_GEM_IS_INSTALLED) do |path|
if FileTest.directory?(path)
$: << File.expand_path(path)
if File.basename(path)[0] == ?. and File.basename(path) != '.'
Find.prune
else
next
end
else
end
end
and after that you can do
require 'dbus'

Have you tried this:
require 'rubygems'
?

Related

Unable to require pry via Bundler.setup for a Ruby application that uses a Gemfile

I'm writing a simple ruby application(basically using PORO's). I've added a Gemfile to it and I'm trying to require the pry gem(which is useful for adding breakpoints while debugging, as the application grows) via the Gemfile but I'm unable to require that gem using Bundler.setup, things work fine with Bundler.require.
I'm trying to avoid the use of Bundler.require for reasons stated in this blog for instance.
In the Employee.rb file, I have the below code -
# require 'bundler'
# require 'bundler/setup'
# Bundler.setup(:default, :test, :development)
Bundler.require(:default, :development, :test)
def total_score(scores)
binding.pry #added on purpose , just to see if the app stops at this breakpoint
scores.inject(:+)
end
When I use Bundler.setup(uncommenting the lines commented above), instead of Bundler.require, I get the below error for the command rspec spec . given from my apps root directory-
Failure/Error: expect(total_score(scores)).to eq(16)
NoMethodError:
undefined method `pry' for #<Binding:0x007fbda22b79d0>
# ./Employee.rb:9:in `total_score'
# ./spec/employee_spec.rb:5:in `block (3 levels) in <top (required)>'
Also, just one more note, as per the bundler docs, I tried even doing a require rubygems, but even that doesn't help. Ideally, I would want to avoid using require rubygems for reasons mentioned in this link.
How do I get my app to require the pry gem using Bundler.setup, without doing a require rubygems alongside and also without using Bunder.require ?
Bundler.setup will simply add the gems to the load path, whereas Bundler.require will do both the adding to the load path and the require.
I recommend using Bundler.setup so that your code boots faster. Then when you need to require pry, do this:
require 'pry'

Require fails with a TypeError 'is not a class' when running rake via RubyGems wrapper

I have a ruby extension written in C++, P4, and it seems to generally work:
I can run irb -Ilib and then require 'P4', and use it
I can execute tests via rake by accessing the shell script in the bin folder of the rake gem, e.g., ${GEM_HOME}/gems/rake-10.3.2/bin/rake test
however, when I access rake via the RubyGems wrapper in my path, e.g., rake test, I get this TypeError
/Users/tjuricek/dev/p4ruby/lib/P4.rb:38:in `require': P4 is not a class (TypeError)
from /Users/tjuricek/dev/p4ruby/lib/P4.rb:38:in `<top (required)>'
from /Users/tjuricek/dev/p4ruby/test/testlib.rb:31:in `require'
from /Users/tjuricek/dev/p4ruby/test/testlib.rb:31:in `<top (required)>'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:15:in `require'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:15:in `block in <main>'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:4:in `select'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Then it pops out the ruby command that "failed". If I copy and paste that command and run it, it works.
What I've noticed is that RubyGems creates a fairly simple wrapper script:
#!/usr/bin/env ruby_executable_hooks
#
# This file was generated by RubyGems.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
version = $1
ARGV.shift
end
end
gem 'rake', version
load Gem.bin_path('rake', 'rake', version)
I'm guessing that the last line, load Gem.bin_path... has triggered some kind of misconfiguration of my part in creating my extension, but I have no idea what that would be.
Does anyone have ideas on what might cause require to fail only when run under the RubyGems wrapper?
OK, the way to debug this is to go into a line where you load the file:
require 'P4.so'
And see if it's defined:
puts "P4 #{P4.class}"
require 'P4.so'
In this case, when running under rake directly, it loaded the .gemspec which pulled in a version definition that created (incorrectly, in my case) a P4 module:
module P4
version = VERSION = '3000.0.0.pre0'
end
So, for me the fix included:
Changing module P4 to class P4
Requiring the version definition before my require 'P4.so' statement, typically: require_relative 'P4/version'
Not defining the class in my C++ extension code, but loading it and extending the one defined in my version file:
// Ensure the class has been defined by the version specification
cP4 = rb_path2class("P4");

Manipulating Zip archives with Ruby 1.8.7

I'm relatively new to Ruby, and I have to use the version 1.8.7.
I have tried 'zip' and ended up with a cryptic stack trace while attempting to use the example from the docs. My script
#!/usr/bin/env ruby
require 'rubygems'
require 'zip'
require 'zip/zipfilesystem'
Zip::ZipFile.open("myfile.war") do |a|
puts a.file.read("META-INF/context.xml")
end
produces the following:
$./rzip.rb
/var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `dup': can't dup NilClass (TypeError)
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `dup'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `map'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1137:in `dup'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1373:in `initialize'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1384:in `new'
from /var/lib/gems/1.8/gems/zip-2.0.2/lib/zip/zip.rb:1384:in `open'
from ./rzip.rb:7
What is wrong with my code? Or is it some version incompatibility issue? If it is not my blunder, what should I use for zip archives manipulations?
I can't reproduce your error although it seems you are not alone in this. Can I suggest using Zip::ZipFile. This is running on my system (Ubuntu 12.04 LTS):
require 'zip/zip'
Zip::ZipFile.open("my.zip", Zip::ZipFile::CREATE) {
|zipfile|
zipfile.get_output_stream("first.txt") { |f| f.puts "Hello from ZipFile" }
zipfile.mkdir("a_dir")
}
You can also see see some good examples/usage of Zip::ZipFile on GitHub

RUBY 2.0.0 How to run a file in irb, so that all its methods are parts of irb environment?

I know the command for this is: require file name
but I guess this command is for ruby 1.9.4 and I am using ruby 2.0.0
the exact message is.
$require start.rb
LoadError: cannot load such file -- start.rb
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from (irb):2
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'
Thanks in advance
Inside irb
>> require './start'
=> true
The file start.rb being in the same folder as you are, and the required file being in quotes, prefaced with the relative location ./ as shown.
This should work for you in Ruby from 1.8 on to 2.0 and beyond.
According to the docs, require will look for start.rb in the $LOAD_PATH since it does not resolve to an absolute path. In this case, the LoadError is telling you that start.rb is not in your $LOAD_PATH.
I am guessing that start.rb is in your current directory. You can use
>> require '/path/to/start.rb'
or
>> require './start.rb'
or
>> require './start'
This saves you some typing:
$ irb -r ./start.rb

gem-install of mongoid throws an uninitialized constant in Ruby, works in irb

I am writing a script with Ruby/MongoDB that stores Tweets. After I gem-installed mongoid, this first-steps code throws an error:
require 'rubygems'
require 'mongo'
require 'mongoid'
Mongoid.database = Mongo::Connection.new('localhost').db('db')
# snippet from http://rujmah.posterous.com/using-mongoid-without-rails
NB. This is no Rails app, but a Terminal script.
The error I get is:
./mongoid.rb:10: uninitialized constant Mongoid (NameError)
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
from mongoid.rb:3
It works in irb and I'm running ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0].
What am I doing wrong?
Edit August 2012
Somehow I got it to work. Alas, nearly a year on, I really can’t remember how. I will try to do better next time.
I hit the same issue while trying to get Bullet gem to work. The solution for me was to simply move gem 'mongoid', github: 'mongoid/mongoid' as the first line of the Gemfile. I find it really odd but that was how I got rid of that error.
I am using Ruby 2.1.0 and Rails 4.0.0

Resources