Requiring ActiveRecord on IRB - Ruby (NO Rails) - ruby

How can I load ActiveRecord on an IRB session?
I have the following
# config/app.rb
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'db/mydb.sqlite3'
)
But when I start IRB and try to load it
irb#1(main):001:0> require config/application.rb
I get
NameError: undefined local variable or method `config' for main:Object
Did you mean? conf
I'd like to be able to interact with my ActiveRecord objects from IRB.
I'm NOT using Rails but only ActiveRecord.
Thanks

Two things to change here:
Always put quotes around the path you're requiring. The reason Ruby is saying "undefined local variable or method" is that it's trying to interpret config as a variable name. Put the name in quotes and it won't.
Use require_relative when loading files that are part of your application. require only looks in the default Ruby load paths.
Try this:
require_relative 'config/application.rb'

You can use pry to build a console started from command line. Simple console solution below. This way you don't have to require in irb every time you stare interactive session.
# bin/console
#!/usr/bin/env ruby
require_relative '../config/app.rb'
require 'pry'
binding.pry
More about pry https://github.com/pry/pry
P.S. You should set +x on bin/console, i.e.
$ chmod +x bin/console
Then you just call
$ bin/console
and get all the code run from config/app.rb and interactive session ready. No need to require anything from irb to start working.
Poor-man's rails console equiv. :-)

Related

Bundler issue when running a ruby script

I am trying to run some older ruby script (with old ruby version) from within a ruby script. Here is a program:
old_ruby187.rb
#!/usr/ruby/1.8.7/bin/ruby
puts "Hello"
new_ruby230.rb
#!/usr/ruby/2.3.0/bin/ruby
require "rubygems"
require "bundler"
Bundler.setup # Code works if I comment this line
puts `old_ruby187.rb`
I get bundler load error. If I execute ./new_ruby230.rb, it gives an error for the last puts command line.:
'require': no such file to load -- rubygems (LoadError)
If I comment just Bundler.setup and run it, it works fine. Not sure if Bundler.setup tries to load something for system call. I need bundler for other gems used in new_ruby230.rb script.
Any help is appreciated.
Update (02/22/2018):
I ended up using ssh when calling old ruby script. Something like:
new_ruby230.rb
#!/usr/ruby/2.3.0/bin/ruby
require "rubygems"
require "bundler"
require "socket"
Bundler.setup
puts `ssh #{Socket.gethostname} old_ruby187.rb` # this worked!
Pretty sure you're not supposed to require rubygems like that. Bundler is there to manage your gems. Remove that require statement (require "rubygems") and you should be good to go.

There is any kind of dry-run command using ActiveRecord?

I am doing a ruby script using ActiveRecords.
I have tried using
ActiveRecord::Base.logger = Logger.new(STDOUT)
and now I can see what happens, but there is some way to NOT execute the command, just see that it happened?
Not using rails,rake,etc... Just a ruby script.
Any help is welcome.
maybe try this
gem install pry
in your script
require 'pry'
in your script you can now stop with
binding.pry

Rackup: cannot load such file 'sinatra'

I already installed sinatra gem and in irb, if I type,
require 'sinatra'
It returns true.
But when I do
rackup -d config.ru
It tells me
nil
Exception `LoadError' at /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36 - cannot load such file -- sinatra
Here is my config.ru
require './app'
set :environment, ENV['RACK_ENV'].to_sym
set :app_file, 'app.rb'
disable :run
run Sinatra::Application
app.rb
require 'rubygems'
require 'sinatra'
get '' do
'Hello World'
end
I don't know what is going wrong.
$ which ruby
/usr/local/bin/ruby
$ which rackup
/usr/local/bin/rackup
$ ruby -v
ruby 1.9.3p374 (2013-01-15 revision 38858) [x86_64-linux]
$ rackup -v
Rack 1.2 (Release: 1.5)
I think this is just the verbose output from setting the -d option. Does the server actually start (after producing a load of output)?
What’s happening is this. Using -d sets Ruby’s $DEBUG flag to true. Rack then tries to load the app through the config.ru, which in turn loads your app.rb. The first thing in app.rb is require 'sinatra'.
Rubygems replaces the original require method with its own. When you call require it tries to load the file normally using the existing load path and the original require method. If the gem hasn’t been loaded this will raise a LoadError, which Rubygems catches before loading the gem.
With the $DEBUG flag set, Ruby will produce a message when an exception is raised even though it is rescued and dealt with, and this is what you’re seeing.
To avoid this simply omit the -d flag to your call to rackup (perhaps enabling warnings with -w would give you a sufficiently verbose output without swamping you in too much detail).
If the server isn’t starting then it will be a different issue rather than not finding Sinatra. If that is the case you’ll need to check the rest of the output for clues.
(Note that I originally thought something else was happening, and that’s what my questions int he comments were about.)
my guess is that your rackup script is a binstub of a 'rack' gem
installed in a diff ruby1.9x vm
maybe earlier version of ruby1.9.2
so it can't see the sinatra installed
I'd try 'which rackup' on the command line
This is definitely issue of load path. Anyway try to setup required ruby and gems via RVM and Bundler. It makes sure that Ruby interpreter and load paths are consistent.

Command Line Arguments with Sinatra

I have a Sinatra program that I am creating, and I would like to be able to pass in command line arguments to this app when I launch it. The problem that I have is that I'm not sure how to do this. I've tried Trollop and looked at OptParser. Trollop doesn't appear to work with Sinatra because OptParser seems to be "default" parser with Sinatra. Is this true? If so, how can I customize the types of arguments accepted when I launch my app?
ruby app.rb hello
# app.rb
require 'sinatra'
get '/' do
ARGV[0]
end
Now when I visit localhost:4567 (where Thin hosts my sinatra app), I see a page that says hello.
Alternatively, you can use environment variables.
Example borrowed from here: https://gist.github.com/benlovell/351962
require 'rubygems'
require 'sinatra'
get '/' do
ENV['envvar']
end
Then run:
envvar=something ruby app.rb

Can not find gems running "ruby <my_script>.rb" – but works in IRB

I am experiencing issues when I am trying to run my .rb-file with the Ruby-command trying to access a gem. The gem i am trying to use is Ruby-Whois. I have an example script below that when I try to execute it through "ruby whois.rb" I get this error message:
./whois.rb:6: uninitialized constant Whois (NameError)
However, if I run the same script line by line in IRB I get the expected result. What may cause this?
Below is whois.rb
require "rubygems"
require "whois"
domain = "google.com"
c = Whois::Client.new
a = c.query(domain)
puts a
change the name of your file - there is ambiguity in require 'whois' and ruby is requireing your file instead of a gem. when you do it line by line in irb ruby knows what you exactly want to require, so everything works.

Resources