Sinatra cannot find views on Ruby 1.9.2-p0 - ruby

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.

Related

RubyGems - require, file location and (load error) complications

very new to coding so, having exhausted Google and Stack Overflow, would really appreciate some advice...
I am currently building a web-scraper to get familiar with CMD vs Sublime Text, feeling Ruby in action; So i am working my way through this tutorial
After having actioned in CMD
C:\gem install HTTParty
SUBLIME TEXT - starts with this code:
require_relative 'HTTParty'
require_relative 'Nokogiri'
etc
But before i can get to anything more from CMD, i hit web_scraper.rb and it returns with:
C:/Users/ATH18/Desktop/nokogiri_tutorial/web_scraper.rb:1:in `require_relative': cannot load such file -- C:/Users/ATH18/Desktop/nokogiri_tutorial/httparty (LoadError)
from C:/Users/ATH18/Desktop/nokogiri_tutorial/web_scraper.rb:1:in `<main>'
[Finished in 0.1s with exit code 1]
I think this has to be due to one of the following:
i) maybe gems have to have their actual files dragged into whatever folder you're creating a new program in?
ii) i'm missing another piece of information that would let it run properly?
iii) perhaps there's another way to tell CMD/ruby that the "require"d gem is not in the current folder (I read this somewhere but their advice didnt seem to work either).
NOTE - i have done gem install xxxxxx in both the C:\ directory and C:\users\desktop\projectFolder\
Help?
You have to use require instead of require_relative. The difference between both a is explained here: https://stackoverflow.com/a/3672600/92049
Use require 'GEMNAME' for gems installed with gem install GEMNAME; use require_relative 'PATH' to require a file relative to the file containing require_relative. (Most often you will find yourself using require.)
To come back to your question: As it says in the tutorial, you have to write require 'HTTParty' instead of require_relative 'HTTParty'.
Does this answer your original question?

How to use pry-byebug in an example script for a gem?

I'm working on making my first gem, which is not a Rails app, is a tic-tac-toe library with some AI in it, so I can play a computer opponent that will never lose and force a win if possible.
Right now I am trying to debug the attack strategy in the AI, but I can't seem to figure out how to get pry-byebug working in my test script, specfically, have the debugging commands like step, next, etc. work upon hitting a binding.pry.
The gem, named smart-tac-toe, has the following directory structure:
$ ls smart-tac-toe
example Gemfile Gemfile.lock Guardfile lib LICENSE.txt Rakefile README.md smart_tac_toe.gemspec spec tmp
As you can see above, there is an 'example' directory in my gem which contains "example.rb", where I use the classes I've made.
However, when I use binding.pry and try to use step and next, the Pry session just exits and the script keeps running.
In my smart_tac_toe.gemspec file, I clearly have pry-byebug:
spec.add_development_dependency "pry-byebug", '~>2.0.0'
and at the top of my example.rb file, I have tried requiring the proper gems:
require 'pry'
require 'pry-byebug'
require "../lib/smart_tac_toe.rb"
I am using Ruby 2.1.1p76 , the repo for this gem is located at https://github.com/discotroll65/smart_tac_toe
Also, though putting binding.pry into my example script does throw me into a debugging session, initially it is in a reading mode, and I have to press q to exit that before I can start doing repl stuff. Any thoughts as to why this may be?
Ok, looking into this more I realized (I think...still kind of new to the game) a couple things --
1.) If you want to if have
require 'pry'
at the top of your ruby file and have it work in general, it would help to install it in your development environment using your terminal:
user#machine/currentdirectory/$ gem install pry
likewise with pry-byebug:
user#machine/currentdirectory/$ gem install pry-byebug
2.) The real answer to my initial question is to use
byebug
in my script as the debugging hook, instead of
binding.pry
(thanks #mtm for the suggestion!)
when I do use byebug though, while step and next work properly, the REPL it throws me into doesn't have any color, and isn't as nice in general...anyway to fix that?
I think you're overdoing it. This works for me:
require 'pry-byebug'
puts 'foo'
binding.pry
puts 'bar'

Ruby on Windows path for requires in dir not working

I've a small ruby program that require files in the same directory. Program works perfect on my mac and when I run a test ruby script without any require it also works so. It seems the ruby program doesn't look in the current directory for the file by default. e.g. the . dir. In windows where do I need to update this so ruby does look in the current dir for requires?
Chances are that your Mac is running Ruby 1.8 and Windows is running Ruby 1.9. As of 1.9, the default load path no longer includes the current directory. A common practice is to add this to the top of your ruby file before your require statements
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'my_file.rb'
You can also use the shorthand $: instead of $LOAD_PATH:
$:.unshift File.dirname(__FILE__)
Another alternative is adding the load path on the command line instead:
ruby -I. my_ruby_file.rb
Ok, I understand now since 1.9.2 for "Security" reasons they don't allow require to work like that anymore. The neatest way I found to solve it strangely was to put './' in front of every require.
e.g.
require "./myfile.rb"
"." was removed from $: was removed from Ruby 1.9.2 to be precise. Do
puts RUBY_VERSION
puts $:.inspect
on Ruby 1.8 (what's installed on your Mac) and Ruby 1.9.2 (what's installed on your windows machine) if you don't believe me.
Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative? discusses why "." was removed.

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

Ruby "No Such File To Load - sqlite3" on OS X

I was trying to create a quick little script that would insert data into an SQLite db for me but I can't get past the first few steps.
I have done "sudo gem install sqlite3-ruby", my Ruby version is 1.8.7 (I used the Ruby one click installer from rubyforge.org).
My script is incredibly simple. It looks like this:
#!/usr/bin/ruby -w
require "csv.rb"
require "sqlite3"
begin
CSV.open('updateddata.sql', 'r') do |row|
p row
end
rescue => err
puts "Exception : #{err}"
err
end
It never makes it past the line require "sqlite3". It just errors and tells me that it can't find that file to load.
I don't understand how it won't work even after using the One click installer (which is supposed to have SQLite built into the install).
I'm not even sure where to go from here.
I am not a Ruby developer at all, I just wanted to use it as a learning experience and to quickly complete this task for myself.
Dunno if Ruby in OS X behaves differently, but normally you need to do require 'rubygems' before requiring any gems.
You can also add
export RUBYOPT=rubygems
to your profile.
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/43fc65132487f98e/?pli=1 using sqlite3-ruby gem solved my issue on this

Resources