Errors in the Sinatra 'hello world' - ruby

Doing the getting started of Sinatra.
I get this error:
./sinatra.rb:5: undefined method `get' for main:Object (NoMethodError)
from /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from sinatra.rb:3
Googling on these errors returns ruby LoadError: cannot load such file which I don't see how that relates to Sinatra.
Not sure what other info I need to share to make my question clearer. So just tell me what other commands I should run to make the question clear.
UPDATE: Actual code
# sinatra.rb
require 'rubygems'
require 'sinatra'
get '/' do
'hey girl'
end

The problem here is due to you naming your file sinatra.rb. When you run that file, the first thing it does is require 'sinatra', and since the current directory is on the load path in Ruby 1.8.7, it tries to load itself. It then gets to the call to get '/' do ..., but since the real Sinatra hasn’t been loaded this results in the error.
The fix is to rename your file to something other than sinatra.rb, you could use myapp.rb as suggested in page you linked to.

Related

Require in ruby

I have the following ruby code:
require 'locationclass'
I have installed the gem locationclass, but it's still giving me the error message:
LoadError: cannot load such file -- locationclass
from C:/Ruby/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/Ruby/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from (irb):1
from C:/Ruby/bin/irb.cmd:19:in `<main>'
Does anyone know how to fix this?
require command loads files, not gems. As I see the gem "locationclass" has only one file in its lib folder, named main.rb. So to load it you need to call:
require 'main'
Also, it's a bad style to have different names for gem and its main file. Not to mention that name 'main' is too generic.

Ruby SketchUp LoadError if requiring correct files

I have the problem that I want to require a ruby file.
File overview:
ExcelConversion.rb
main.rb
/classes/excelReader.rb
/classes/elementIdentifier.rb
the main.rb has the header:
require classes/excelReader
require classes/elementIdentifier
the excelReader.rb has the header:
require 'rubyXL
the elementIdentifier.rb has the header:
require 'inifile
The problem is that if I execute the main.rb file, everything works fine. But, as soon as I write require main within the ExcelConversion file I get the error:
Error Loading File ExcelConversion.rb
Error: #<LoadError: cannot load such file -- rubyXL>
C:/Program Files (x86)/SketchUp/SketchUp 2018/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:54:in 'require'
C:/Program Files (x86)/SketchUp/SketchUp 2018/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:54:in 'require'
C:/shortenedPath/ExcelConversion/classes/ExcelReader.rb:1:in '<top (required)>'
C:/shortenedPath/ExcelConversion/main.rb:1:in 'require_relative'
C:/shortenedPath/ExcelConversion/main.rb:1:in '<top (required)>'
C:/shortenedPath/ExcelConversion.rb:4:in 'require_relative'
C:/shortenedPath/ExcelConversion.rb:4:in '<top (required)>'
I don't understand how that is even possible to throw an error, since the main.rb runs without problems.
Edit: Solution
I managed to find the solution. SketchUp needs to install the gems separately. This can be achieved through the Ruby Console and the command:
Gem.install "nameOfTheGem"
Then SketchUp stores a copy of the gem within it's own path and one can require it as usual.

Ruby Unit Tests fail in Eclipse on OS X

I'm finally getting around to learning Ruby using Programming Ruby 1.9. I have installed Ruby using Brew on OS X and it installed Ruby 2.0.
Most of the exercises have worked fine in Eclipse, but when I got to the first example that creates a unit test I started running into trouble.
In Eclipse, I created a test case like:
require_relative 'WordsFromString'
require 'minitest/autorun'
class TestWordsFromString < Test::Unit::TestCase
...
end
When I run it as a Ruby Test, I get the following output:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- test/unit/ui/console/testrunner (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Applications/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/1276/0/.cp/testing/dltk-testunit-runner.rb:1:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
I think there might be something wrong with my basic Ruby installation because if I try and run it from the command line I get:
Asok:work_freq jeff$ ruby TestWordsFromString.rb
TestWordsFromString.rb:4:in `<main>': uninitialized constant Test (NameError)
EDIT: With the help of Ursus I have figured out that as long as the require and base class are consistent the command line works.
So either of the following allow the command-line to successfully execute the test case.
require 'minitest/autorun'
class TestWordsFromString < MiniTest::Unit::TestCase
or
require 'test/unit'
class TestWordsFromString < Test::Unit::TestCase
So now I just need to get this working inside Eclipse.
Try
class TestWordsFromString < Minitest::Test
In searching around for a answer, I came across this Ruby issue
that seems to be relevant. Since I'm new to Ruby I'm not sure I fully understood it, but what I got out of it was that there was an unexpected behavior related to installing the gem test-unit that existed in Ruby 1.9. I hadn't installed any gems, so I tried:
gem install test-unit
after which the unit test executed successfully in Eclipse and on the command-line.

Failed to load gem within passenger app but gem loads in irb

I've deployed a few rails apps but just infrequently enough to feel like every time is my first time. This time I'm installing a sinatra app.
I'm using mod_passenger and I'm getting a problem with a missing gem. However it's there.
From the error in the browser I see:
no such file to load -- json (LoadError)
./application.rb:10:in `require'
./application.rb:10
config.ru:1:in `require'
config.ru:1
/usr/lib64/ruby/gems/1.8/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
/usr/lib64/ruby/gems/1.8/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
config.ru:1:in `new'
config.ru:1
The top of the file looks like this:
require 'rubygems'
require 'sinatra'
require 'json'
require 'sequel'
require 'sinatra/sequel'
(there are comments above these lines, hence the line number differences)
When I open irb and try to require the gems it works:
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'json'
=> true
Sadly, I'm using RHEL 6.2 which is different enough from Ubuntu that I'm a little uncomfortable. But based on my understanding this should work.
Any ideas on things I could look for to see what could cause my problem?
are you sure that passenger is running the same ruby version like irb?

Why do I get NameError when I run a rack app with "rackup"?

I'm having trouble running a rack app using rackup.
This is my config.ru:
$:.unshift "#{File.dirname(__FILE__)}/lib/"
require 'blogrite'
run Blogrite::Server.new(:with => :test)
Calling rackup config.ru throws:
[~/Code/blogrite master] rackup config.ru
/Users/josemota/Code/blogrite/lib/blogrite/server.rb:2:in `<module:Blogrite>': uninitialized constant Blogrite::Server (NameError)
from /Users/josemota/Code/blogrite/lib/blogrite/server.rb:1:in `<top (required)>'
from /Users/josemota/Code/blogrite/config.ru:5:in `block in <main>'
...
You can check the full project in Github. I do not understand the reason I get NameError.
I have found the reason for this error to come up. The NameError shown is fired from the module, which won't recognize the paths correctly.
#Dogbert correctly pointed out that autoload was probably not working. According to Dave Barker in his post, the class that autoloads must include the current path in $: / $LOAD_PATH. I did that and now it works.
The resulting commit is now available. Thanks #Dogbert for pointing the autoload issue in the first place!

Resources