Manually connect to PostgreSQL with Ruby - ruby

Connecting to postgres with rails was no big deal. rails -d postgresql app_name, setup the database.yml and voila. I cannot, however, use postgres with just a little test script
I've tried installing the gems postgres, dbi & dbd-pg, pg, and ruby-pg, but with pg/ruby-pg and postgres it fails at the require for require 'postgres' or require 'pg'. With require 'dbi' I get past the require, but it can't load the driver....so how is rails doing it with the same set of packages? In fact I removed all the afore mentioned and found I only needed the 'pg' gem for everything to work fine with rails. Any advice?

Are you remembering to add a require 'rubygems' to your source, or invoke ruby with a -rubygems argument, or add RUBYOPT=rubygems to your environment? You need to do one of those to actually load the gem machinery that allows require to find your gems.

Related

JDBC connectivity using ruby not jruby

Is there any gem available to Connect to JDBC DB2 for the Ruby project. I have come across activerecord-jdbc-adapter but it works only for Jruby.
Also can i use Jruby specific gem in Ruby? I have Ruby 2.2 and Jruby 9.0.0 in my machine
Did it using two other gems which was mentioned in one site.
We need Sequel and ibm_db gems and ran the below code, it worked
require 'rubygems'
require 'sequel'
require 'ibm_db'
Sequel.connect("ibmdb://#{username}:#{password}##{db_url}") do |db|
db.fetch(selectquery) do |row|
puts row
end
end

Ruby: can't require installed gem 'stopwords-filter'

I'm trying to use this this gem, but even though it shows up with a gem list after I install it, a require 'stopwords-filter' in irb results in LoadError: cannot load such file -- stopwords-filter.
To make sure that I was actually able to install and use gems, I also tried installing this gem and I can require it just fine. Everything works.
What am I missing about stopwords-filter?
Thanks!
Even though the gem is named stopwords-filter, the require statement is just stopwords:
require 'stopwords'

Require a gem library

I'm kinda new to ruby and I'd like to use a gem charting library, but for some reason when I require it in the ruby script on my desktop it doesn't work. However when I require in my irb it does work. Is there a way to fix this?
Try using bundler if you can and declare the requirements in a Gemfile. This will make your environment much more consistent between different computers and will provide a reference of the dependencies you have.
The Bundler setup procedure is pretty simple and well documented. It will load in all the gems and any of their dependencies automatically.
Generally the problem with the require statement failing is the library is not in your $LOAD_PATH, and that's usually because you haven't loaded rubygems:
require 'rubygems'
gem 'somegem'
require 'somegem'

Bundler: how to use without rails?

I have a project using cucumber outside of rails. How can I load the gems with the versions specified in my gemfile?
Digging through the Bundler website:
Create Gemfile (run bundle init to create skeleton Gemfile)
bundle install
In your app:
# Only needed for ruby 1.8.x
require 'rubygems'
# The part that activates bundler in your app
require 'bundler/setup'
# require your gems as usual
require 'some_gem'
# ...or require all the gems in one statement
Bundler.require
Could be worth checking out:
Bundler.io - Using Bundler in Your Appplication
Bundler.io - Bundler.setup and Bundler.require
Are bundle exec and require 'bundler/setup' equivalent?
I just learned about a way to make Bundler automatically require dependencies from a Gemfile. Add this code at the beginning of a Ruby program that has a Gemfile:
require 'rubygems'
require 'bundler/setup'
Bundler.require
With Bundler.require there's no need to explicitly require the gems/libraries enumerated in the Gemfile.
This solution is from http://technotales.wordpress.com/2010/08/22/bundler-without-rails/
To be honest I'm not sure if the require rubygems part is needed either.
Here's the simplest and most straightforward approach:
bundler init will create the Gemfile for you
Specify gems in the Gemfile.
Add the following to your main Ruby file
require 'bundler/setup'
Bundler.require
Run bundler install to install the gems.
More information can (now) be found at http://bundler.io.
Casper has a pretty good answer (despite some passive aggressiveness) but I think the missing piece for you is bundle exec. When you run the $ rails ... commands on the command line, Rails uses bundler to load those dependencies/gems. Rake, for example, doesn't by default so in order to run rake test using an older version of cucumber than what is on your system, you have to use bundle exec rake test. It's a good habit to get into always using $ bundle exec ... when you're using Bundler — it's explicit, you're always sure you're using the right gems, and it ensures you don't forget to add a dependency to your Gemfile (i.e. you push to another server or another developer and they are having issues because you didn't note the need for something you use but they don't).

Given a ruby script how to figure out what it depends on?

I want to distribute a ruby script to many of my friends, because it's useful. But how do I know what else they might have to install? I mean at the top of the script, there is this:
require 'rubygems' #
require 'activerecord' #TODO: figure out what packages this depends on
require 'activesupport' #
require 'duration' #
That gives me some idea about what they need to install, but last time I tried it on a friend's computer(Linux) each of the above turned out to require move packages. For example, activesupport requires a database, which in case of this script is sqlite3, so I had to install sqlite3 and a bunch of lib and maybe even dev packages.
Is there any tool or method to gather up a list of all the dependencies so I can include them in installation instructions? Or even better, is there a way to package them into an easy installator?
Distribute it as a gem. The gem lets you add dependencies, and if a dependency has a dependency, the rubygems system will install it for you.
If you are requiring activerecord, you need some sort of activerecord adapter driver installed or the gem corresponding to the db, e.g. pg, mysql, sqlite-ruby as well as a connection set up to connect to said db.
Whenever you install gems using the current rubygems they will install dependencies, its just that activerecord is a bit ... "funny" ?

Resources