Question

I'm building a project in Ruby using Sinatra in Windows 7.

My project structure is:

/project 
  |_ one.rb
  |_ sin.rb

So, my code in one.rb:

require 'sinatra'
require 'sin'

But when I run it, the console puts:

C:/RailsInstaller/Ruby1.9.3/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:
36:in `require': cannot load such file -- sin (LoadError)
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/site_ruby/1.9.1/rubygems/custo
m_require.rb:36:in `require'
        from one.rb:1:in `<main>'

How can I fix it?

Was it helpful?

Solution 3

Also you can put the sin.rb into the lib/ folder, then just run:

bundle install

and then:

bundle exec project/one.rb

Expanded version

Make sure that you properly made the Gemfile, and .gemspec. The templates for the files can be automatically generated by bundler as follows:

bundle gem your_gem_name

Then you shell create the folders bin/, and lib/, put sin.rb into lib/, and one.rb into bin/. And fill all TODOs in the .gemspec the with proper values:

spec.summary       = %q{short summary}
spec.description   = %q{longer description}

Then run bundle install, and exec as it was specified above. And in case if you will generate the gem, and then install it with gem install, you'll not need to change anything.

OTHER TIPS

You have to use require_relative:

require_relative 'sin'

Or include the current directory in your $LOAD_PATH:

$LOAD_PATH << '.'
require 'sin'

Try require_relative 'sin'. It will work. Read the documentation of Kernel#require_relative

Read this post What is the difference between require_relative and require in Ruby? to know more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top