0

i am trying to write a gem using bundler (following ryan's railcast - 245)
i followed everything described by ryan. I also added 4 ruby files in the lib folder to learn about how require works with different gem.

i thought it will automatically require those rb files put in lib folder. but unfortunately while i test it it throws an error saying no such file to load lib/myclass.rb
This what my lorem.rb looks like.

require 'lorem/version'  
require 'lorem/myclass'  

Module Lorem  
.....  
end  

What am i missing ? can any body tell how gem require works ?

NB : I use to do it with echoe and it works . but when i am using bulder, gemspec and all i simply dont understand why its not loading files .

My gem Spec file

# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "lorem/version"

Gem::Specification.new do |s|
  s.name        = "lorem"
  s.version     = Lorem::VERSION
  s.authors     = ["My Name"]
  s.email       = ["me@domain.com"]
  s.homepage    = ""
  s.summary     = %q{learing bundler gem}
  s.description = %q{Learing bundler gem}

  s.rubyforge_project = "lorem"

  s.files         = `git ls-files`.split("\n")
  s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
  s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
  s.require_paths = ["lib"]

  #s.add_development_dependency "rspec"
  #s.add_development_dependency "supermodel"
end

thanks in advance

Krishna Prasad Varma
  • 4,670
  • 5
  • 29
  • 41
  • where does this myclass.rb come from? it is not there in the ryan's railcast - 245 description. Try if the file exists and also try my_class.rb – Mithun Sasidharan Dec 26 '11 at 07:21
  • myclass.rb is a file which i added in lib/lorem folder. i tried to write the code there in myclass.rb and use it in lorem.rb by requiring it . but it didnt work. – Krishna Prasad Varma Dec 26 '11 at 08:46
  • http://stackoverflow.com/questions/4516533/a-problem-with-the-require-keyword-not-finding-the-file-to-load – Mithun Sasidharan Dec 26 '11 at 08:47
  • when i inspect the gems folder i see only version.rb in the lib folder. why is that it doesnt copy other ruby files in the lib when doing rake install ? should i need to mention it explicitly anywhere? – Krishna Prasad Varma Dec 26 '11 at 14:26

2 Answers2

1

In your gemspec, try replacing $:.push File.expand_path("../lib", __FILE__) with:

$:.unshift('lib')
buruzaemon
  • 3,847
  • 1
  • 23
  • 44
0

Finally Cracked it.

lib = File.expand_path('../lib/', __FILE__)  
$:.unshift lib unless $:.include?(lib) 

and giving the following inside

Gem::Specification.new do |s|

  ...
  s.files        = Dir.glob("{bin,lib}/**/*")
  ...

end

I got this hint from http://yehudakatz.com/2010/04/02/using-gemspecs-as-intended/

Krishna Prasad Varma
  • 4,670
  • 5
  • 29
  • 41