MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

Archive for the ‘Ruby’ Category

Ruby+PostgreSQL on Ubuntu

without comments

This extends the earlier post on installing and configuring Ruby 3.3.0 on Ubuntu 22.0.4. Please refer to that earlier post to install Ruby. This post shows you how to install the necessary libraries and Ruby Gems for PostgreSQL.

You need to install the libra-dev package, as shown:

sudo apt install postgresql libpq-dev

Next, you need to install the PG Gem:

gem install pg

You can now write a postgres_version.rb program to verify a connection to the PostgreSQL database, like:

# Include Ruby Gem libraries.
require 'rubygems'
require 'pg'
 
# Begin block.
begin
  # Create a new connection resource.
  db = PG::connect( 'localhost', 5432, '', '', 'videodb', 'student', 'student')
 
  # Create a result set.
  stmt = db.query('SELECT version() AS version')
 
  # Read through the result set hash.
  stmt.each do | row |
    puts "#{row['version']}"
  end
  # Release the result set resources.
  stmt.freeze
 
rescue PG::Error => e
  # Print the error.
  puts "ERROR #{e.error} (#{e.sqlstate})"
  puts "Can't connect to the PostgreSQL database specified."
  # Signal an error.
  exit 1
 
ensure
  # Close the connection when it is open.
  db.close if db
end

Call the postgres_version.rb program with this syntax:

ruby mysql_version.rb

It should return:

PostgreSQL 14.10 (Ubuntu 14.10-0ubuntu0.22.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, 64-bit

The postgres_columns.rb script returns a couple columns concatenated into a single column:

# Include Ruby Gem libraries.
require 'rubygems'
require 'pg'
 
# Begin block.
begin
  # Create a new connection resource.
  db = PG::connect( 'localhost', 5432, '', '', 'videodb', 'student', 'student')
 
  # Create a result set.
  stmt = db.query("SELECT   CONCAT(nh.last_name, ', ', nh.first_name) AS name " + \
                  "FROM     new_hire nh " +                                       \
                  "ORDER BY nh.last_name")
 
  # Read through the result set hash.
  stmt.each do | row |
    out = ""
    i = 0
    while i < stmt.fields.count()
      # Check when not last column and use the:
      #   - Hash returned by the result set for the value, and
      #   - String array value returned by the statement object
      #     as the name value of the hash by leveraging its 
      #     numeric index.
      if i < stmt.fields.count() - 1
        out += "#{row[stmt.fields[i]]}"
        out += ", "
      else
        out += "#{row[stmt.fields[i]]}"
      end
      i += 1
    end
    puts "#{out}"
  end
 
  # Release the result set resources.
  stmt.freeze
 
rescue PG::Error => e
  # Print the error.
  puts "ERROR #{e.error} (#{e.sqlstate})"
  puts "Can't connect to PostgreSQL database specified."
  # Signal an error.
  exit 1
 
ensure
  # Close the connection when it is open.
  db.close if db
end

Call the postgres_columns.rb program with this syntax:

ruby mysql_columns.rb

It should return:

Chabot, Henry
Lewis, Malcolm

As always, I hope this helps those looking to learn and solve a problem. You can find the PG Gem documentation here.

Written by maclochlainn

February 7th, 2024 at 12:15 am

Ruby+MySQL on Ubuntu

without comments

This post goes through installing and configuring Ruby and Ruby on Rails for MySQL. The first step requires updating the Ubuntu OS:

sudo apt-get update

Interestingly, I found that the man-db service had inadvertently stopped. It raised the following error:

E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem.

You run this command to find the problem with the dpkg utility:

sudo dpkg --configure -a

It returned:

Setting up man-db (2.10.2-1) ...
Updating database of manual pages ...
man-db.service is a disabled or a static unit not running, not starting it.

The following command started the man-db service:

sudo systemctl start man-db.service

Next, you install the prerequisite packages with this command:

sudo apt-get install -y git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev

Use the cd command to change to the student home directory. Clone the asdf as the multiple runtime version manager with this command:

git clone https://github.com/excid3/asdf.git ~/.asdf

The following is the output of the git clone command:

Cloning into '/home/student/.asdf'...
remote: Enumerating objects: 8756, done.
remote: Counting objects: 100% (829/829), done.
remote: Compressing objects: 100% (476/476), done.
remote: Total 8756 (delta 428), reused 657 (delta 334), pack-reused 7927
Receiving objects: 100% (8756/8756), 3.10 MiB | 4.29 MiB/s, done.
Resolving deltas: 100% (5148/5148), done.

Next, you fix your .bashrc file by adding the following components:

echo '. "$HOME/.asdf/asdf.sh"' >> ~/.bashrc
echo '. "$HOME/.asdf/completions/asdf.bash"' >> ~/.bashrc
echo 'legacy_version_file = yes' >> ~/.asdfrc
echo 'export EDITOR="code --wait"' >> ~/.bashrc

Source the modifies shell, which you can do like this:

exec $SHELL

or, like:

. ${HOME}/.bashrc

Add the following asdf plug-ins:

asdf plugin add ruby
asdf plugin add nodejs

Install Ruby with the following command:

asdf install ruby 3.3.0

Install Ruby Global with this syntax:

asdf global ruby 3.3.0

Update the Ruby Gems with this command:

gem update --system

You can confirm your Ruby install with two commands. First, use the which utility to check the Ruby install:

which -a ruby

It should return:

/home/student/.asdf/shims/ruby

Then, check the Ruby version:

ruby -v

It should return:

ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]

Assuming you’ve installed and configured MySQL 8 on Ubuntu, you need this additional library to support the necessary Ruby Gem:

sudo apt-get install -y libmysqlclient-dev

Now, you can install the current MySQL Ruby Gem:

gem install mysql2

You can now write a mysql_connection.rb program to verify a connection to the MySQL 8 database, like:

# Include Ruby Gem libraries.
require 'rubygems'
require 'mysql2'
 
begin
  # Create new database connection.
  db = Mysql2::Client.new( :host     => 'localhost' \
                         , :username => 'student'   \
                         , :password => 'student'   \
                         , :database => 'studentdb')
 
  # Create a result set.
  stmt = db.query('SELECT version() AS version')
 
  # Read through the result set hash.
  stmt.each do | row |
    puts "#{row['version']}"
  end
  # Release the result set resources.
  stmt.free
 
rescue Mysql2::Error => e
  # Print the error.
  puts "ERROR #{e.errno} (#{e.sqlstate}): #{e.error}"
  puts "Can't connect to the MySQL database specified."
  # Signal an error.
  exit 1
 
ensure
  # Close the connection when it is open.
  db.close if db
end

Call the program with this syntax:

ruby mysql_connection.rb

It should return:

Connected to the MySQL database server.

You can verify the version with this mysql_version.rb program:

# Include Ruby Gem libraries.
require 'rubygems'
require 'mysql2'
 
begin
  # Create new database connection.
  db = Mysql2::Client.new( :host     => 'localhost' \
                         , :username => 'student'   \
                         , :password => 'student'   \
                         , :database => 'studentdb')
 
  # Create a result set.
  rs = db.query('SELECT version() AS version')
 
  # Read through the result set hash.
  rs.each do | row |
    puts "#{row['version']}"
  end
 
  # Release the result set resources.
  rs.free
 
rescue Mysql2::Error => e
  # Print the error.
  puts "ERROR #{e.errno} (#{e.sqlstate}): #{e.error}"
  puts "Can't connect to the MySQL database specified."
  # Signal an error.
  exit 1
 
ensure
  # Close the connection when it is open.
  db.close if db
end

On Ubuntu, it should return:

8.0.35-0ubuntu0.22.04.1

If you don’t know anything about the mysql2 Ruby Gem, you should read the documentation. It’s very concise and requires a basic understanding of Ruby programming. The two specific pages who may want to check for the next examples are:

The mysql_version.rb version uses the known string literal for columns or column aliases returned by the SQL statement, which becomes the stmt (or statement) in the program. The next program eliminates the need to enumerate with the text-based columns from the query by using the Statement#fields array values by use of a numeric index. The numeric index returns the field names from the Statement#fields class to use in as the name for values in the Result#fields value found in the row variable of the for loop.

# Include Ruby Gem libraries.
require 'rubygems'
require 'mysql2'
 
# Begin block.
begin
  # Create a new connection resource.
  db = Mysql2::Client.new( :host     => 'localhost' \
                         , :username => 'student'   \
                         , :password => 'student'   \
                         , :database => 'studentdb')
 
  # Create a result set.
  stmt = db.query("SELECT   DISTINCT i.item_title, ra.rating " +       \
                  "FROM     item i INNER JOIN rating_agency ra " +     \
                  "ON       i.item_rating_id = ra.rating_agency_id " + \
                  "WHERE    ra.rating_agency = 'MPAA'" +               \
                  "ORDER BY 1")
 
  # Read through the result set hash.
  stmt.each do | row |
    out = ""
    i = 0
    while i < stmt.fields.count()
      # Check when not last column and use the:
      #   - Hash returned by the result set for the value, and
      #   - String array value returned by the statement object
      #     as the name value of the hash by leveraging its 
      #     numeric index.
      if i < stmt.fields.count() - 1
        out += "#{row[stmt.fields[i]]}"
        out += ", "
      else
        out += "#{row[stmt.fields[i]]}"
      end
      i += 1
    end
    puts "#{out}"
  end
 
  # Release the result set resources.
  stmt.free
 
rescue Mysql2::Error => e
  # Print the error.
  puts "ERROR #{e.errno} (#{e.sqlstate}): #{e.error}"
  puts "Can't connect to MySQL database specified."
  # Signal an error.
  exit 1
 
ensure
  # Close the connection when it is open.
  db.close if db
end

It returns the select two columns from the query:

A Man for All Seasons, G
Around the World in 80 Days, G
Beau Geste, PG
Brave Heart, R
Camelot, G
Casino Royale, PG-13
...
Tomorrow Never Dies, PG-13
Tora! Tora! Tora!, G
Tron, PG

The following mysql_query_params.rb Ruby example accepts a single argument to leverage a wild card query in MySQL:

require 'rubygems'
require 'mysql2'
 
# Input external arguments.
arguments = ARGV
 
# Check for one input parameter and substitute an empty string
# when one isn't found.
if arguments.length == 1
  argument = arguments[0]
else
  argument = ""
end
 
# Begin block.
begin
  # Create a new connection resource.
  db = Mysql2::Client.new( :host     => 'localhost' \
                         , :username => 'student'   \
                         , :password => 'student'   \
                         , :database => 'studentdb')
 
  # Create a result set.
  stmt = db.prepare("SELECT   DISTINCT i.item_title, ra.rating " +       \
                    "FROM     item i INNER JOIN rating_agency ra " +     \
                    "ON       i.item_rating_id = ra.rating_agency_id " + \
                    "WHERE    ra.rating_agency = 'MPAA'" +               \
                    "AND      i.item_title LIKE CONCAT(?,'%')" +         \
                    "ORDER BY 1")
 
  # Bind the variable into the query.
  rs = stmt.execute(argument)
 
  # Read through the result set hash.
  rs.each do | row |
    out = ""
    i = 0
    while i < rs.fields.count()
      # Check when not last column and use the:
      #   - Hash returned by the result set for the value, and
      #   - String array value returned by the statement object
      #     as the name value of the hash by leveraging its 
      #     numeric index.
      if i < rs.fields.count() - 1
        out += "#{row[rs.fields[i]]}"
        out += ", "
      else
        out += "#{row[rs.fields[i]]}"
      end
      i += 1
    end
    puts "#{out}"
  end
 
  # Release the result set resources.
  rs.free
 
rescue Mysql2::Error => e
  # Print the error.
  puts "ERROR #{e.errno} (#{e.sqlstate}): #{e.error}"
  puts "Can't connect to MySQL database specified."
  # Signal an error.
  exit 1
 
ensure
  # Close the connection when it is open.
  db.close if db
end

If you call the mysql_query_params.rb program with this syntax:

ruby mysql_aquery_params.rb Harry

It’ll return the following from the studentdb database:

Harry Potter and the Chamber of Secrets, PG
Harry Potter and the Deathly Hallows, Part 1, PG-13
Harry Potter and the Deathly Hallows, Part 2, PG-13
Harry Potter and the Goblet of Fire, PG-13
Harry Potter and the Half Blood Prince, PG
Harry Potter and the Order of the Phoenix, PG-13
Harry Potter and the Prisoner of Azkaban, PG
Harry Potter and the Sorcerer's Stone, PG

After that, you should install Rails (check for current version beyond 1/2024). Install Ruby Global with this syntax:

gem install rails -v 7.1.3

Check the version installed:

rails -v

It should return:

Rails 7.1.3

Run this command to enable Rails for MySQL 8:

rails new myapp -d mysql

If you want to configure a username and password for MySQL, edit the config/database.yml file.

As always, I hope this helps somebody looking for step-by-step guide.

Written by maclochlainn

February 3rd, 2024 at 4:57 pm

Ruby GEM Mongo

without comments

While trying to use the Ruby gem utility to install the MongoDB gem, I encountered an error on a new Fedora 27 instance. This is the error message:

Fetching: bson-4.3.0.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mongo:
        ERROR: Failed to build gem native extension.
 
    current directory: /usr/local/share/gems/gems/bson-4.3.0/ext/bson
/usr/bin/ruby -r ./siteconf20180517-49401-1htl7zc.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/share/include/ruby.h
 
extconf failed, exit code 1
 
Gem files will remain installed in /usr/local/share/gems/gems/bson-4.3.0 for inspection.
Results logged to /usr/local/lib64/gems/ruby/bson-4.3.0/gem_make.out
[student@localhost ~]$ ruby --version
ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-linux]

There wasn’t much on the error but I checked the Ruby installation and ruby-devel package wasn’t installed by default. That’s odd since I choose to install the development components on the workstation.

Not a problem, I simply ran the yum utility as root through a sudoer user to install the ruby-devel package:

yum install -y ruby-devel

You should see a successful installation log like:

As I suspected, it fixed the problem immediately when I ran gem utility to install the mongo gem. The syntax to install the mongo gem is:

gem install mongo

The console output should be:

Building native extensions.  This could take a while...
Successfully installed bson-4.3.0
Fetching: mongo-2.5.3.gem (100%)
Successfully installed mongo-2.5.3
Parsing documentation for bson-4.3.0
Installing ri documentation for bson-4.3.0
Parsing documentation for mongo-2.5.3
Installing ri documentation for mongo-2.5.3
Done installing documentation for bson, mongo after 3 seconds
2 gems installed

You can now write and run a Ruby test MongoDB connection program. Here’s a basic MongoDBTest.rb Ruby file:

#!/usr/bin/ruby
 
# Require libraries.
require 'rubygems'
require 'mongo'
 
# Create a new connection to MongoDB.
$client = Mongo::Client.new(['localhost'])
 
puts 'Connected to MongoDB'
puts '===================='
puts 'Database Names:'
puts '---------------'
$client.database_names.each{|name| puts name}

After you create the MongoDBTest.rb file, you need to change its permissions with the chmod utility as follows:

chmod 755 MongoDBTest.rb

Then, you can run it as follows from the directory where you created the file:

./MongoDBTest.rb

Unless you’ve added something to the base MongoDB instance, it should print:

Connected to MongoDB
====================
Database Names:
---------------
admin
local

As always, I hope this helps somebody looking for a straightforward example and solution.

Written by maclochlainn

May 17th, 2018 at 2:00 pm

Oracle EBS 12.2 & Ruby

without comments

As does seem to occur from time-to-time, I’m out there in the weeds again and sorting out a solution that fits a customer’s need. They wanted to know if they could write Oracle EBS 12.2 Concurrent Manager Programs in Ruby. They don’t want to write Java.

I checked the documentation, which as is too common, didn’t answer the question. I’m sure if I downloaded the PDF and searched it for one of the languages I knew Oracle supported, I would have found the list of supported languages.

It was simply quicker to query the Oracle EBS 12.2 FND_LOOKUPS table like so:

SELECT   lookup_type
,        lookup_code
,        SUBSTR(meaning,1,30) AS meaning
FROM     fnd_lookups
WHERE    lookup_type = 'CP_EXECUTION_METHOD_CODE'
ORDER BY meaning;

It returns the list of possible types of Oracle EBS 12.2 Concurrent Manager Programs:

LOOKUP_TYPE		   LOOKUP_CODE	MEANING
-------------------------- ------------ ------------------------------
CP_EXECUTION_METHOD_CODE   X		FlexRpt
CP_EXECUTION_METHOD_CODE   F		FlexSql
CP_EXECUTION_METHOD_CODE   H		Host
CP_EXECUTION_METHOD_CODE   S		Immediate
CP_EXECUTION_METHOD_CODE   K		Java Concurrent Program
CP_EXECUTION_METHOD_CODE   J		Java Stored Procedure
CP_EXECUTION_METHOD_CODE   M		Multi Language Function
CP_EXECUTION_METHOD_CODE   P		Oracle Reports
CP_EXECUTION_METHOD_CODE   I		PL/SQL Stored Procedure
CP_EXECUTION_METHOD_CODE   E		Perl Concurrent Program
CP_EXECUTION_METHOD_CODE   B		Request Set Stage Function
CP_EXECUTION_METHOD_CODE   L		SQL*Loader
CP_EXECUTION_METHOD_CODE   Q		SQL*Plus
CP_EXECUTION_METHOD_CODE   R		SQL*Report
CP_EXECUTION_METHOD_CODE   Z		Shutdown Callback
CP_EXECUTION_METHOD_CODE   A		Spawned

That gave me some of the answer. You can’t call Ruby programs directly. However, Perl lets you use Inline::Ruby. You can use Inline:Ruby to call your Ruby programs. So, if you use Perl to wrap Ruby you don’t have to use Java.

Written by maclochlainn

August 23rd, 2016 at 11:50 am

IT Salary Thought

with 2 comments

During the holidays, I check salaries for my students and the IT industry overall. I’m never surprised by the reality, after all salaries pay for return on skills and effort. Here’s my annual look, which some may find unkind but reality is seldom kind.

Before looking at IT salaries, it seems like a good opportunity to first look at the overall job market for Millennials in the United States. AOL provides a great graphic of the median income for Millennials (those born between 1981 and 1997), which is $18,000 to $43,000 a year:

millennial-median-income-state-map

That’s a stark contrast to Forbes’ statistics on the top college baccalaureate degrees. In fact, the top five with the highest salary are between $58 to $67 thousand a year. They are:

  1. Computer Science ………… $66,800
  2. Engineering ………………… $65,000
  3. Mathematics & Statistics … $60,300
  4. Economics ………………….. $58,600
  5. Finance ……………………… $58,000

Computer science, applied computer science, and information technology are probably lumped into the first category. Information systems, exposure without real skills, is a management degree and probably opens positions equivalent to the business degree at $50 thousand a year. More or less, that’s a nine thousand dollar difference between having real skills and being able to talk the game and supervise technical resources. (The 10 hottest IT skills for 2015 are listed in Computerworld.)

There’s no surprise that Ruby, Objective C (iPhone, iPad, Mac OS X), Python, Java, C++ are at the top of the pyramid. Starting salaries in the Salt Lake area are higher for programmers college than they are for other computer science skill sets. In fact, my informal contacts peg them as starting at $70+ thousand. That’s higher than Forbes average for computer science. Here’s a visual on experienced programmers by language:

2015LanguageSalaries

It seems fair to say that a computer science, applied computer science, and information technology degree with an emphasis in real programming skills is the best bet to pay off student loans. However, some will wait for politicians to do that for them, but really that’s quite unlikely, isn’t it?

Reality is always blunt. Reality also seems to frequently differs from what politicians say. After all, politicians pander to audiences, which generally means they say a great deal of nonsense. Nonsense like economics doesn’t matter, everyone should earn the same regardless of their education, skills, or work ethic. Aldous Huxley said it more elegantly when he said, “That all men are equal is a proposition to which, at ordinary times, no sane human being has ever given his assent.”

Written by maclochlainn

December 21st, 2015 at 6:24 pm

Ruby Thin Web Server

without comments

Somebody suggested that I try out thin, “A fast and very simple Ruby web server.” So, I thought it might be interesting to test, and a simplification over Rails to demonstrate an small Ruby MVC pattern.

Installing thin seemed straight forward as a gem installation, like

gem install thin

The initial install didn’t work out of the box because I’d neglected to install the gcc-c++ library. It raised the following errors:

Fetching: eventmachine-1.0.7.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing thin:
	ERROR: Failed to build gem native extension.
 
    /usr/bin/ruby extconf.rb
checking for main() in -lssl... no
checking for rb_trap_immediate in ruby.h,rubysig.h... no
checking for rb_thread_blocking_region()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for inotify_init() in sys/inotify.h... yes
checking for writev() in sys/uio.h... yes
checking for rb_thread_fd_select()... yes
checking for rb_fdset_t in ruby/intern.h... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_enable_interrupt()... no
checking for rb_time_new()... yes
checking for sys/event.h... no
checking for epoll_create() in sys/epoll.h... yes
checking for clock_gettime()... yes
checking for CLOCK_MONOTONIC_RAW in time.h... yes
checking for CLOCK_MONOTONIC in time.h... yes
creating Makefile
 
make "DESTDIR="
g++ -I. -I/usr/include -I/usr/include/ruby/backward -I/usr/include -I. -DWITHOUT_SSL -DBUILD_FOR_RUBY -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_TBR -DHAVE_RUBY_THREAD_H -DHAVE_RB_THREAD_CALL_WITHOUT_GVL -DHAVE_RB_THREAD_CALL_WITHOUT_GVL -DHAVE_INOTIFY_INIT -DHAVE_INOTIFY -DHAVE_WRITEV -DHAVE_WRITEV -DHAVE_RB_THREAD_FD_SELECT -DHAVE_RB_THREAD_FD_SELECT -DHAVE_TYPE_RB_FDSET_T -DHAVE_RB_FDSET_T -DHAVE_RB_WAIT_FOR_SINGLE_FD -DHAVE_RB_TIME_NEW -DOS_UNIX -DHAVE_EPOLL_CREATE -DHAVE_EPOLL -DHAVE_CLOCK_GETTIME -DHAVE_CONST_CLOCK_MONOTONIC_RAW -DHAVE_CONST_CLOCK_MONOTONIC    -fPIC -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -mtune=generic -m64 -o ed.o -c ed.cpp
make: g++: Command not found
make: *** [ed.o] Error 127
 
 
Gem files will remain installed in /usr/local/share/gems/gems/eventmachine-1.0.7 for inspection.
Results logged to /usr/local/share/gems/gems/eventmachine-1.0.7/ext/gem_make.out

Naturally, I installed the gcc-c++ library with the yum utility, like this:

yum list gcc-c++

It displayed the following log output:

Loaded plugins: langpacks, refresh-packagekit
mysql-connectors-community                                  | 2.5 kB  00:00     
mysql-tools-community                                       | 2.5 kB  00:00     
mysql56-community                                           | 2.5 kB  00:00     
pgdg93                                                      | 3.6 kB  00:00     
updates/20/x86_64/metalink                                  |  14 kB  00:00     
updates                                                     | 4.9 kB  00:00     
updates/20/x86_64/primary_db                                |  13 MB  00:04     
(1/2): updates/20/x86_64/updateinfo                         | 1.9 MB  00:02     
(2/2): updates/20/x86_64/pkgtags                            | 1.4 MB  00:00     
Resolving Dependencies
--> Running transaction check
---> Package gcc-c++.x86_64 0:4.8.3-7.fc20 will be installed
--> Processing Dependency: libstdc++-devel = 4.8.3-7.fc20 for package: gcc-c++-4.8.3-7.fc20.x86_64
--> Running transaction check
---> Package libstdc++-devel.x86_64 0:4.8.3-7.fc20 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package                Arch          Version              Repository      Size
================================================================================
Installing:
 gcc-c++                x86_64        4.8.3-7.fc20         updates        7.2 M
Installing for dependencies:
 libstdc++-devel        x86_64        4.8.3-7.fc20         updates        1.5 M
 
Transaction Summary
================================================================================
Install  1 Package (+1 Dependent package)
 
Total download size: 8.7 M
Installed size: 25 M
Downloading packages:
(1/2): gcc-c++-4.8.3-7.fc20.x86_64.rpm                      | 7.2 MB  00:05     
(2/2): libstdc++-devel-4.8.3-7.fc20.x86_64.rpm              | 1.5 MB  00:01     
--------------------------------------------------------------------------------
Total                                              1.3 MB/s | 8.7 MB  00:06     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Installing : libstdc++-devel-4.8.3-7.fc20.x86_64                          1/2 
  Installing : gcc-c++-4.8.3-7.fc20.x86_64                                  2/2 
  Verifying  : gcc-c++-4.8.3-7.fc20.x86_64                                  1/2 
  Verifying  : libstdc++-devel-4.8.3-7.fc20.x86_64                          2/2 
 
Installed:
  gcc-c++.x86_64 0:4.8.3-7.fc20                                                 
 
Dependency Installed:
  libstdc++-devel.x86_64 0:4.8.3-7.fc20                                         
 
Complete!

After installing the gcc-c++ libraries, I reran the gem utility to install the thin utility. It created three Ruby Gems: eventmachine-1.0.7, daemons-1.2.2.gem, and thin-1.6.3.gem, as shown:

Building native extensions.  This could take a while...
Successfully installed eventmachine-1.0.7
Fetching: daemons-1.2.2.gem (100%)
Successfully installed daemons-1.2.2
Fetching: thin-1.6.3.gem (100%)
Building native extensions.  This could take a while...
Successfully installed thin-1.6.3
Parsing documentation for daemons-1.2.2
Installing ri documentation for daemons-1.2.2
Parsing documentation for eventmachine-1.0.7
Installing ri documentation for eventmachine-1.0.7
Parsing documentation for thin-1.6.3
Installing ri documentation for thin-1.6.3
Done installing documentation for daemons, eventmachine, thin after 11 seconds
3 gems installed

Having created the Ruby Gems, I followed the thin instruction on the web site, and created the following Ruby web server:

app = proc do |env|
  [
    200,             # Status code
    {                # Response headers
      'Content-Type' => 'text/html',
      'Content-Length' => '12',
    },
    ['Hello World!'] # Response body
  ]
end
 
# You can install Rack middlewares
# to do some crazy stuff like logging,
# filtering, auth or build your own.
use Rack::CommonLogger
 
run app

Then, I tested the Ruby web server with the following command:

thin start -R thin.ru

It displayed the following test server as the result of localhost:3000 URL:

ThinRubyWebServer

As always, I hope this helps those who land on this page.

Written by maclochlainn

April 18th, 2015 at 10:00 pm

Ruby-MySQL Columns

with one comment

Last week I posted how to configure and test Ruby and MySQL. Somebody asked me how to handle a dynamic list of columns. So, here’s a quick little program to show you how to read the dynamic list of column (and this updated blog post has the 2024 update for the new Mysql2 ODBC driver):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'rubygems'
require 'mysql'
 
# Begin block.
begin
  # Create a new connection resource.
  db = Mysql.new('localhost','student','student','studentdb')
 
  # Create a result set.
  rs = db.query('SELECT item_title, item_rating FROM item')
  # Read through the result set hash.
  rs.each do | row |
    out = ""
    i = 0
    while i < db.field_count
      # Check if not last column.
      if i < db.field_count - 1
        out += "#{row[i]}, "
      else
        out += "#{row[i]}"
      end
      i += 1
    end
    puts "#{out}"
  end
  # Release the result set resources.
  rs.free
rescue Mysql::Error => e
  # Print the error.
  puts "ERROR #{e.errno} (#{e.sqlstate}): #{e.error}"
  puts "Can't connect to MySQL database specified."
  # Signal an error.
  exit 1
ensure
  # Close the connection when it is open.
  db.close if db
end

The new logic on lines 13 through 22 reads the list of columns into a comma delimited list of values. The if-block checks to make sure it doesn’t append a comma to the last column in the list. It prints output like:

The Hunt for Red October, PG
Star Wars I, PG
Star Wars II, PG
Star Wars II, PG
Star Wars III, PG13
The Chronicles of Narnia, PG
RoboCop, Mature
Pirates of the Caribbean, Teen
The Chronicles of Narnia, Everyone
MarioKart, Everyone
Splinter Cell, Teen
Need for Speed, Everyone
The DaVinci Code, Teen
Cars, Everyone
Beau Geste, PG
I Remember Mama, NR
Tora! Tora! Tora!, G
A Man for All Seasons, G
Hook, PG
Around the World in 80 Days, G
Harry Potter and the Sorcerer's Stone, PG
Camelot, G

As always, I hope this helps those looking for a solution.

Written by maclochlainn

April 18th, 2015 at 2:25 am

Ruby-MySQL Program

with 6 comments

After you install Ruby and build the Rails framework, you need to create the mysql gem. This blog post shows you how to create the mysql gem and how to write a simple Ruby program that queries the MySQL database.

The first step creates the mysql gem for Ruby programming:

gem install mysql

It should show you the following:

Fetching: mysql-2.9.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed mysql-2.9.1
Parsing documentation for mysql-2.9.1
Installing ri documentation for mysql-2.9.1
Done installing documentation for mysql after 0 seconds
1 gem installed

After you install the mysql Ruby Gem, you can write and test a test.rb Ruby program that tests a MySQL database connection. The simplest complete code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Include Ruby Gem libraries.
require 'rubygems'
require 'mysql'
 
begin
  # Create new database connection.
  db = Mysql.new('localhost','student','student','studentdb')
  # Print connected message.
  puts "Connected to the MySQL database server."
rescue Mysql::Error => e
  # Print the error.
  puts "ERROR #{e.errno} (#{e.sqlstate}): #{e.error}"
  puts "Can't connect to the MySQL database specified."
  # Signal an error.
  exit 1
ensure
  # Close the connection when it is open.
  db.close if db
end

You can run the program with the following syntax:

ruby test.rb

The program prints “Connected to the MySQL database server.” when there’s a student user with a student password that’s authorized to connect to the studentdb database. If any of the values are invalid when creating the connection, the program prints “Can’t connect to the MySQL database specified.”

Having tested the connection, the next query.rb program tests the connection by returning values from a query:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Include Ruby Gem libraries.
require 'rubygems'
require 'mysql'
 
# Begin block.
begin
  # Create a new connection resource.
  db = Mysql.new('localhost','student','student','studentdb1')
  # Create a result set.
  rs = db.query('SELECT item_title FROM item')
  # Read through the result set hash.
  rs.each_hash do | row |
    puts "#{row['item_title']}"
  end
  # Release the result set resources.
  rs.free
rescue Mysql::Error => e
  # Print the error.
  puts "ERROR #{e.errno} (#{e.sqlstate}): #{e.error}"
  puts "Can't connect to MySQL database specified."
  # Signal an error.
  exit 1
ensure
  # Close the connection when it is open.
  db.close if db
end

You can test it with the following command-line syntax:

ruby query.rb

It returns a data set like this from the item table of my video store example:

+---------------------------------------+
| item_title                            |
+---------------------------------------+
| The Hunt for Red October              |
| Star Wars I                           |
| Star Wars II                          |
| Star Wars II                          |
| Star Wars III                         |
| The Chronicles of Narnia              |
| RoboCop                               |
| Pirates of the Caribbean              |
| The Chronicles of Narnia              |
| MarioKart                             |
| Splinter Cell                         |
| Need for Speed                        |
| The DaVinci Code                      |
| Cars                                  |
| Beau Geste                            |
| I Remember Mama                       |
| Tora! Tora! Tora!                     |
| A Man for All Seasons                 |
| Hook                                  |
| Around the World in 80 Days           |
| Harry Potter and the Sorcerer's Stone |
| Camelot                               |
+---------------------------------------+
22 rows in set (0.00 sec)

You need the ruby interpreter to run them. You can make the programs standalone operations by putting the following line as the first line in your Ruby programs.

1
#!/usr/bin/ruby

Then, you can run the program like this if they have read and execute privileges and are located in the present working directory where you issue the following command:

./mysql_query.rb

If you want to work with individual columns, please check this subsequent post that shows how you can access individual columns. As always, I hope this helps those trying to get things working.

After posting this somebody asked for books that could help them learn how to write Ruby programs. While books are nice and listed below, I’d start with the tryruby.org web site.

I’d recommend the following books because …

  • The Ruby Programming Language is 7 years old now and only covers Ruby 1.8 and 1.9, but its written by David Flanagan and the creator of the Ruby Programming Language – Yukihiro Matsumoto.
  • Programming Ruby 1.9 & 2.0: The Pragmatic Programmer’s Guide is more current and a well balanced approach at learning how to write Ruby programs.
  • The Well-Grounded Rubyist is the most current book and teaches you how to think about writing Ruby beyond just the syntax. As a Manning book, you can purchase the physical copy and automatically get a downloadable ebook. It’s certainly the best value for the money option provided you already know how to program in at least one other object-oriented programming language.

Written by maclochlainn

April 11th, 2015 at 2:35 am

Install Ruby on Fedora

with 4 comments

I use a Fedora 20 VM image to teach Oracle and MySQL technology. Last week, I expanded the Fedora VM image to support a full LAMP stack. This blog shows you how to install Ruby on Fedora and successfully generate the Rails gems.

Connect as the root user and use yum to install the libraries. My approach is by library or small groups. Naturally, you start with the ruby library.

yum install ruby

You will see the following:

Loaded plugins: langpacks, refresh-packagekit
mysql-connectors-community                                  | 2.5 kB  00:00     
mysql-tools-community                                       | 2.5 kB  00:00     
mysql56-community                                           | 2.5 kB  00:00     
pgdg93                                                      | 3.6 kB  00:00     
updates/20/x86_64/metalink                                  |  14 kB  00:00     
updates                                                     | 4.9 kB  00:00     
(1/3): mysql56-community/20/x86_64/primary_db               |  80 kB  00:00     
(2/3): pgdg93/20/x86_64/primary_db                          |  80 kB  00:00     
(3/3): updates/20/x86_64/primary_db                         |  13 MB  00:06     
(1/2): updates/20/x86_64/pkgtags                            | 1.4 MB  00:01     
(2/2): updates/20/x86_64/updateinfo                         | 1.9 MB  00:01     
Resolving Dependencies
--> Running transaction check
---> Package ruby.x86_64 0:2.0.0.353-16.fc20 will be installed
--> Processing Dependency: ruby-libs(x86-64) = 2.0.0.353-16.fc20 for package: ruby-2.0.0.353-16.fc20.x86_64
--> Processing Dependency: rubygem(bigdecimal) >= 1.2.0 for package: ruby-2.0.0.353-16.fc20.x86_64
--> Processing Dependency: ruby(rubygems) >= 2.0.3 for package: ruby-2.0.0.353-16.fc20.x86_64
--> Processing Dependency: /usr/bin/ruby for package: ruby-2.0.0.353-16.fc20.x86_64
--> Processing Dependency: libruby.so.2.0()(64bit) for package: ruby-2.0.0.353-16.fc20.x86_64
--> Running transaction check
---> Package ruby-libs.x86_64 0:2.0.0.353-16.fc20 will be installed
---> Package rubygem-bigdecimal.x86_64 0:1.2.0-16.fc20 will be installed
---> Package rubygems.noarch 0:2.1.11-115.fc20 will be installed
--> Processing Dependency: rubygem(rdoc) >= 4.0.0 for package: rubygems-2.1.11-115.fc20.noarch
--> Processing Dependency: rubygem(psych) >= 2.0.0 for package: rubygems-2.1.11-115.fc20.noarch
--> Processing Dependency: rubygem(io-console) >= 0.4.1 for package: rubygems-2.1.11-115.fc20.noarch
---> Package rubypick.noarch 0:1.1.1-1.fc20 will be installed
--> Running transaction check
---> Package rubygem-io-console.x86_64 0:0.4.2-16.fc20 will be installed
---> Package rubygem-psych.x86_64 0:2.0.0-16.fc20 will be installed
--> Processing Dependency: libyaml-0.so.2()(64bit) for package: rubygem-psych-2.0.0-16.fc20.x86_64
---> Package rubygem-rdoc.noarch 0:4.0.1-2.fc20 will be installed
--> Processing Dependency: rubygem(json) < 2 for package: rubygem-rdoc-4.0.1-2.fc20.noarch
--> Processing Dependency: rubygem(json) >= 1.4 for package: rubygem-rdoc-4.0.1-2.fc20.noarch
--> Processing Dependency: ruby(irb) for package: rubygem-rdoc-4.0.1-2.fc20.noarch
--> Running transaction check
---> Package libyaml.x86_64 0:0.1.6-2.fc20 will be installed
---> Package ruby-irb.noarch 0:2.0.0.353-16.fc20 will be installed
---> Package rubygem-json.x86_64 0:1.7.7-101.fc20 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package                 Arch        Version                 Repository    Size
================================================================================
Installing:
 ruby                    x86_64      2.0.0.353-16.fc20       updates       65 k
Installing for dependencies:
 libyaml                 x86_64      0.1.6-2.fc20            updates       55 k
 ruby-irb                noarch      2.0.0.353-16.fc20       updates       86 k
 ruby-libs               x86_64      2.0.0.353-16.fc20       updates      2.8 M
 rubygem-bigdecimal      x86_64      1.2.0-16.fc20           updates       77 k
 rubygem-io-console      x86_64      0.4.2-16.fc20           updates       48 k
 rubygem-json            x86_64      1.7.7-101.fc20          fedora        60 k
 rubygem-psych           x86_64      2.0.0-16.fc20           updates       75 k
 rubygem-rdoc            noarch      4.0.1-2.fc20            fedora       288 k
 rubygems                noarch      2.1.11-115.fc20         updates      224 k
 rubypick                noarch      1.1.1-1.fc20            updates      6.3 k
 
Transaction Summary
================================================================================
Install  1 Package (+10 Dependent packages)
 
Total download size: 3.7 M
Installed size: 13 M
Is this ok [y/d/N]: y
Downloading packages:
(1/11): ruby-2.0.0.353-16.fc20.x86_64.rpm                   |  65 kB  00:00     
(2/11): libyaml-0.1.6-2.fc20.x86_64.rpm                     |  55 kB  00:00     
(3/11): ruby-irb-2.0.0.353-16.fc20.noarch.rpm               |  86 kB  00:00     
(4/11): rubygem-io-console-0.4.2-16.fc20.x86_64.rpm         |  48 kB  00:00     
(5/11): rubygem-json-1.7.7-101.fc20.x86_64.rpm              |  60 kB  00:00     
(6/11): rubygem-psych-2.0.0-16.fc20.x86_64.rpm              |  75 kB  00:00     
(7/11): rubypick-1.1.1-1.fc20.noarch.rpm                    | 6.3 kB  00:00     
(8/11): rubygem-bigdecimal-1.2.0-16.fc20.x86_64.rpm         |  77 kB  00:01     
(9/11): rubygem-rdoc-4.0.1-2.fc20.noarch.rpm                | 288 kB  00:00     
(10/11): ruby-libs-2.0.0.353-16.fc20.x86_64.rpm             | 2.8 MB  00:01     
(11/11): rubygems-2.1.11-115.fc20.noarch.rpm                | 224 kB  00:01     
--------------------------------------------------------------------------------
Total                                              1.4 MB/s | 3.7 MB  00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Installing : ruby-libs-2.0.0.353-16.fc20.x86_64                          1/11 
  Installing : libyaml-0.1.6-2.fc20.x86_64                                 2/11 
  Installing : rubygem-bigdecimal-1.2.0-16.fc20.x86_64                     3/11 
  Installing : rubygem-json-1.7.7-101.fc20.x86_64                          4/11 
  Installing : rubygem-psych-2.0.0-16.fc20.x86_64                          5/11 
  Installing : rubygem-rdoc-4.0.1-2.fc20.noarch                            6/11 
  Installing : ruby-irb-2.0.0.353-16.fc20.noarch                           7/11 
  Installing : rubypick-1.1.1-1.fc20.noarch                                8/11 
  Installing : ruby-2.0.0.353-16.fc20.x86_64                               9/11 
  Installing : rubygems-2.1.11-115.fc20.noarch                            10/11 
  Installing : rubygem-io-console-0.4.2-16.fc20.x86_64                    11/11 
  Verifying  : rubygem-io-console-0.4.2-16.fc20.x86_64                     1/11 
  Verifying  : rubygem-rdoc-4.0.1-2.fc20.noarch                            2/11 
  Verifying  : rubygems-2.1.11-115.fc20.noarch                             3/11 
  Verifying  : rubygem-bigdecimal-1.2.0-16.fc20.x86_64                     4/11 
  Verifying  : ruby-libs-2.0.0.353-16.fc20.x86_64                          5/11 
  Verifying  : rubygem-json-1.7.7-101.fc20.x86_64                          6/11 
  Verifying  : rubygem-psych-2.0.0-16.fc20.x86_64                          7/11 
  Verifying  : rubypick-1.1.1-1.fc20.noarch                                8/11 
  Verifying  : ruby-2.0.0.353-16.fc20.x86_64                               9/11 
  Verifying  : libyaml-0.1.6-2.fc20.x86_64                                10/11 
  Verifying  : ruby-irb-2.0.0.353-16.fc20.noarch                          11/11 
 
Installed:
  ruby.x86_64 0:2.0.0.353-16.fc20                                               
 
Dependency Installed:
  libyaml.x86_64 0:0.1.6-2.fc20                                                 
  ruby-irb.noarch 0:2.0.0.353-16.fc20                                           
  ruby-libs.x86_64 0:2.0.0.353-16.fc20                                          
  rubygem-bigdecimal.x86_64 0:1.2.0-16.fc20                                     
  rubygem-io-console.x86_64 0:0.4.2-16.fc20                                     
  rubygem-json.x86_64 0:1.7.7-101.fc20                                          
  rubygem-psych.x86_64 0:2.0.0-16.fc20                                          
  rubygem-rdoc.noarch 0:4.0.1-2.fc20                                            
  rubygems.noarch 0:2.1.11-115.fc20                                             
  rubypick.noarch 0:1.1.1-1.fc20                                                
 
Complete!

After you install ruby, you need to install the MySQL and Ruby development libraries, like this:

yum -y install gcc mysql-devel ruby-devel rubygems
Loaded plugins: langpacks, refresh-packagekit
Package gcc-4.8.3-7.fc20.x86_64 already installed and latest version
Package rubygems-2.1.11-115.fc20.noarch already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-devel.x86_64 0:5.6.24-1.fc20 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) = 5.6.24-1.fc20 for package: mysql-community-devel-5.6.24-1.fc20.x86_64
---> Package ruby-devel.x86_64 0:2.0.0.353-16.fc20 will be installed
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.6.23-1.fc20 will be updated
--> Processing Dependency: mysql-community-libs(x86-64) = 5.6.23-1.fc20 for package: mysql-community-client-5.6.23-1.fc20.x86_64
---> Package mysql-community-libs.x86_64 0:5.6.24-1.fc20 will be an update
--> Processing Dependency: mysql-community-common(x86-64) = 5.6.24-1.fc20 for package: mysql-community-libs-5.6.24-1.fc20.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.6.23-1.fc20 will be updated
--> Processing Dependency: mysql-community-client(x86-64) = 5.6.23-1.fc20 for package: mysql-community-server-5.6.23-1.fc20.x86_64
---> Package mysql-community-client.x86_64 0:5.6.24-1.fc20 will be an update
---> Package mysql-community-common.x86_64 0:5.6.23-1.fc20 will be updated
---> Package mysql-community-common.x86_64 0:5.6.24-1.fc20 will be an update
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.6.23-1.fc20 will be updated
---> Package mysql-community-server.x86_64 0:5.6.24-1.fc20 will be an update
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package                 Arch    Version               Repository          Size
================================================================================
Installing:
 mysql-community-devel   x86_64  5.6.24-1.fc20         mysql56-community  3.4 M
 ruby-devel              x86_64  2.0.0.353-16.fc20     updates            125 k
Updating for dependencies:
 mysql-community-client  x86_64  5.6.24-1.fc20         mysql56-community   19 M
 mysql-community-common  x86_64  5.6.24-1.fc20         mysql56-community  258 k
 mysql-community-libs    x86_64  5.6.24-1.fc20         mysql56-community  2.0 M
 mysql-community-server  x86_64  5.6.24-1.fc20         mysql56-community   55 M
 
Transaction Summary
================================================================================
Install  2 Packages
Upgrade             ( 4 Dependent packages)
 
Total download size: 80 M
Downloading packages:
No Presto metadata available for mysql56-community
(1/6): mysql-community-common-5.6.24-1.fc20.x86_64.rpm      | 258 kB  00:01     
(2/6): mysql-community-devel-5.6.24-1.fc20.x86_64.rpm       | 3.4 MB  00:01     
(3/6): mysql-community-libs-5.6.24-1.fc20.x86_64.rpm        | 2.0 MB  00:00     
(4/6): ruby-devel-2.0.0.353-16.fc20.x86_64.rpm              | 125 kB  00:00     
(5/6): mysql-community-client-5.6.24-1.fc20.x86_64.rpm      |  19 MB  00:09     
(6/6): mysql-community-server-5.6.24-1.fc20.x86_64.rpm      |  55 MB  00:21     
--------------------------------------------------------------------------------
Total                                              3.3 MB/s |  80 MB  00:24     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Updating   : mysql-community-common-5.6.24-1.fc20.x86_64                 1/10 
  Updating   : mysql-community-libs-5.6.24-1.fc20.x86_64                   2/10 
  Updating   : mysql-community-client-5.6.24-1.fc20.x86_64                 3/10 
  Updating   : mysql-community-server-5.6.24-1.fc20.x86_64                 4/10 
  Installing : mysql-community-devel-5.6.24-1.fc20.x86_64                  5/10 
  Installing : ruby-devel-2.0.0.353-16.fc20.x86_64                         6/10 
  Cleanup    : mysql-community-server-5.6.23-1.fc20.x86_64                 7/10 
  Cleanup    : mysql-community-client-5.6.23-1.fc20.x86_64                 8/10 
  Cleanup    : mysql-community-libs-5.6.23-1.fc20.x86_64                   9/10 
  Cleanup    : mysql-community-common-5.6.23-1.fc20.x86_64                10/10 
  Verifying  : mysql-community-client-5.6.24-1.fc20.x86_64                 1/10 
  Verifying  : mysql-community-devel-5.6.24-1.fc20.x86_64                  2/10 
  Verifying  : ruby-devel-2.0.0.353-16.fc20.x86_64                         3/10 
  Verifying  : mysql-community-libs-5.6.24-1.fc20.x86_64                   4/10 
  Verifying  : mysql-community-common-5.6.24-1.fc20.x86_64                 5/10 
  Verifying  : mysql-community-server-5.6.24-1.fc20.x86_64                 6/10 
  Verifying  : mysql-community-client-5.6.23-1.fc20.x86_64                 7/10 
  Verifying  : mysql-community-server-5.6.23-1.fc20.x86_64                 8/10 
  Verifying  : mysql-community-libs-5.6.23-1.fc20.x86_64                   9/10 
  Verifying  : mysql-community-common-5.6.23-1.fc20.x86_64                10/10 
 
Installed:
  mysql-community-devel.x86_64 0:5.6.24-1.fc20                                  
  ruby-devel.x86_64 0:2.0.0.353-16.fc20                                         
 
Dependency Updated:
  mysql-community-client.x86_64 0:5.6.24-1.fc20                                 
  mysql-community-common.x86_64 0:5.6.24-1.fc20                                 
  mysql-community-libs.x86_64 0:5.6.24-1.fc20                                   
  mysql-community-server.x86_64 0:5.6.24-1.fc20                                 
 
Complete!

After installing ruby, exit the root account to your management account and run the following command from the Linux shell:

ruby -v

It should show you:

ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]

Before you can run gem to install rails, you must install another the libxml2-devel library. Here’s the syntax to install the libxml2-devel library:

yum install libxml2-devel

You should see the following, which includes typing a y to continue:

Loaded plugins: langpacks, refresh-packagekit
Resolving Dependencies
--> Running transaction check
---> Package libxml2-devel.x86_64 0:2.9.1-3.fc20 will be installed
--> Processing Dependency: zlib-devel for package: libxml2-devel-2.9.1-3.fc20.x86_64
--> Processing Dependency: xz-devel for package: libxml2-devel-2.9.1-3.fc20.x86_64
--> Running transaction check
---> Package xz-devel.x86_64 0:5.1.2-12alpha.fc20 will be installed
---> Package zlib-devel.x86_64 0:1.2.8-3.fc20 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package             Arch         Version                   Repository     Size
================================================================================
Installing:
 libxml2-devel       x86_64       2.9.1-3.fc20              updates       1.0 M
Installing for dependencies:
 xz-devel            x86_64       5.1.2-12alpha.fc20        updates        45 k
 zlib-devel          x86_64       1.2.8-3.fc20              fedora         50 k
 
Transaction Summary
================================================================================
Install  1 Package (+2 Dependent packages)
 
Total download size: 1.1 M
Installed size: 9.1 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): xz-devel-5.1.2-12alpha.fc20.x86_64.rpm               |  45 kB  00:00     
(2/3): zlib-devel-1.2.8-3.fc20.x86_64.rpm                   |  50 kB  00:00     
(3/3): libxml2-devel-2.9.1-3.fc20.x86_64.rpm                | 1.0 MB  00:04     
--------------------------------------------------------------------------------
Total                                              264 kB/s | 1.1 MB  00:04     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Installing : zlib-devel-1.2.8-3.fc20.x86_64                               1/3 
  Installing : xz-devel-5.1.2-12alpha.fc20.x86_64                           2/3 
  Installing : libxml2-devel-2.9.1-3.fc20.x86_64                            3/3 
  Verifying  : xz-devel-5.1.2-12alpha.fc20.x86_64                           1/3 
  Verifying  : libxml2-devel-2.9.1-3.fc20.x86_64                            2/3 
  Verifying  : zlib-devel-1.2.8-3.fc20.x86_64                               3/3 
 
Installed:
  libxml2-devel.x86_64 0:2.9.1-3.fc20                                           
 
Dependency Installed:
  xz-devel.x86_64 0:5.1.2-12alpha.fc20     zlib-devel.x86_64 0:1.2.8-3.fc20    
 
Complete!
yum install libxslt-devel

You should see the following and will need to reply with a y during install:

Loaded plugins: langpacks, refresh-packagekit
mysql-connectors-community                                  | 2.5 kB  00:00     
mysql-tools-community                                       | 2.5 kB  00:00     
mysql56-community                                           | 2.5 kB  00:00     
pgdg93                                                      | 3.6 kB  00:00     
updates/20/x86_64/metalink                                  |  14 kB  00:00     
updates                                                     | 4.9 kB  00:00     
updates/20/x86_64/primary_db                                |  13 MB  00:07     
updates/20/x86_64/pkgtags      FAILED                                           
http://mirror.utexas.edu/fedora/linux/updates/20/x86_64/repodata/fe40e35e0289ae1470dbe8030c09b8046924cbaa5e16ac61e9411ac57477820b-pkgtags.sqlite.gz: [Errno 14] HTTP Error 404 - Not Found
Trying other mirror.
(1/2): updates/20/x86_64/updateinfo                         | 1.9 MB  00:02     
(2/2): updates/20/x86_64/pkgtags                            | 1.4 MB  00:00     
Resolving Dependencies
--> Running transaction check
---> Package libxslt-devel.x86_64 0:1.1.28-5.fc20 will be installed
--> Processing Dependency: libgcrypt-devel for package: libxslt-devel-1.1.28-5.fc20.x86_64
--> Running transaction check
---> Package libgcrypt-devel.x86_64 0:1.5.3-2.fc20 will be installed
--> Processing Dependency: libgpg-error-devel for package: libgcrypt-devel-1.5.3-2.fc20.x86_64
--> Running transaction check
---> Package libgpg-error-devel.x86_64 0:1.12-1.fc20 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package                  Arch         Version               Repository    Size
================================================================================
Installing:
 libxslt-devel            x86_64       1.1.28-5.fc20         fedora       309 k
Installing for dependencies:
 libgcrypt-devel          x86_64       1.5.3-2.fc20          fedora       127 k
 libgpg-error-devel       x86_64       1.12-1.fc20           fedora        16 k
 
Transaction Summary
================================================================================
Install  1 Package (+2 Dependent packages)
 
Total download size: 451 k
Installed size: 2.6 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): libgcrypt-devel-1.5.3-2.fc20.x86_64.rpm              | 127 kB  00:00     
(2/3): libgpg-error-devel-1.12-1.fc20.x86_64.rpm            |  16 kB  00:00     
(3/3): libxslt-devel-1.1.28-5.fc20.x86_64.rpm               | 309 kB  00:00     
--------------------------------------------------------------------------------
Total                                              454 kB/s | 451 kB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Installing : libgpg-error-devel-1.12-1.fc20.x86_64                        1/3 
  Installing : libgcrypt-devel-1.5.3-2.fc20.x86_64                          2/3 
  Installing : libxslt-devel-1.1.28-5.fc20.x86_64                           3/3 
  Verifying  : libgcrypt-devel-1.5.3-2.fc20.x86_64                          1/3 
  Verifying  : libgpg-error-devel-1.12-1.fc20.x86_64                        2/3 
  Verifying  : libxslt-devel-1.1.28-5.fc20.x86_64                           3/3 
 
Installed:
  libxslt-devel.x86_64 0:1.1.28-5.fc20                                          
 
Dependency Installed:
  libgcrypt-devel.x86_64 0:1.5.3-2.fc20 libgpg-error-devel.x86_64 0:1.12-1.fc20
 
Complete!

One more to go. You can’t run the Ruby gem utility to create the nokogiri Ruby Gem on Fedora because of a library mismatch. If you attempt to create the Rails framework, like this:

gem install rails

It’ll raise the following error message on trying to dynamically link the nokogiri Ruby Gem. The error will be something like this, and unfortunately, the log files won’t be too useful:

Running patch with /usr/local/share/gems/gems/nokogiri-1.6.6.2/ports/patches/libxml2/0001-Revert-Missing-initialization-for-the-catalog-module.patch...
Running 'patch' for libxml2 2.9.2... ERROR, review '/usr/local/share/gems/gems/nokogiri-1.6.6.2/ext/nokogiri/tmp/x86_64-redhat-linux-gnu/ports/libxml2/2.9.2/patch.log' to see what happened.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

The error message isn’t very helpful but the fix is fortunately easy. You install the nokogiri Ruby Gem directly with the yum utility. The following instructs yum to proceed without waiting for you to type a y to install.

yum install -y rubygem-nokogiri
Loaded plugins: langpacks, refresh-packagekit
Resolving Dependencies
--> Running transaction check
---> Package rubygem-nokogiri.x86_64 0:1.6.6.2-1.fc20 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package                Arch         Version                Repository     Size
================================================================================
Installing:
 rubygem-nokogiri       x86_64       1.6.6.2-1.fc20         updates       534 k
 
Transaction Summary
================================================================================
Install  1 Package
 
Total download size: 534 k
Installed size: 834 k
Downloading packages:
rubygem-nokogiri-1.6.6.2-1.fc20.x86_64.rpm                  | 534 kB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Installing : rubygem-nokogiri-1.6.6.2-1.fc20.x86_64                       1/1 
  Verifying  : rubygem-nokogiri-1.6.6.2-1.fc20.x86_64                       1/1 
 
Installed:
  rubygem-nokogiri.x86_64 0:1.6.6.2-1.fc20                                      
 
Complete!

Now you can use the Ruby gem utility to create the Rails framework like this:

gem install rails

This will take a couple minutes typically, so be patient. You see something like this, dependent on the release:

Fetching: loofah-2.0.1.gem (100%)
Successfully installed loofah-2.0.1
Fetching: rails-html-sanitizer-1.0.2.gem (100%)
Successfully installed rails-html-sanitizer-1.0.2
Fetching: rails-deprecated_sanitizer-1.0.3.gem (100%)
Successfully installed rails-deprecated_sanitizer-1.0.3
Fetching: rails-dom-testing-1.0.6.gem (100%)
Successfully installed rails-dom-testing-1.0.6
Fetching: builder-3.2.2.gem (100%)
Successfully installed builder-3.2.2
Fetching: erubis-2.7.0.gem (100%)
Successfully installed erubis-2.7.0
Fetching: actionview-4.2.1.gem (100%)
Successfully installed actionview-4.2.1
Fetching: actionpack-4.2.1.gem (100%)
Successfully installed actionpack-4.2.1
Fetching: activemodel-4.2.1.gem (100%)
Successfully installed activemodel-4.2.1
Fetching: arel-6.0.0.gem (100%)
Successfully installed arel-6.0.0
Fetching: activerecord-4.2.1.gem (100%)
Successfully installed activerecord-4.2.1
Fetching: globalid-0.3.5.gem (100%)
Successfully installed globalid-0.3.5
Fetching: activejob-4.2.1.gem (100%)
Successfully installed activejob-4.2.1
Fetching: mime-types-2.4.3.gem (100%)
Successfully installed mime-types-2.4.3
Fetching: mail-2.6.3.gem (100%)
Successfully installed mail-2.6.3
Fetching: actionmailer-4.2.1.gem (100%)
Successfully installed actionmailer-4.2.1
Fetching: rake-10.4.2.gem (100%)
Successfully installed rake-10.4.2
Fetching: thor-0.19.1.gem (100%)
Successfully installed thor-0.19.1
Fetching: railties-4.2.1.gem (100%)
Successfully installed railties-4.2.1
Fetching: bundler-1.9.2.gem (100%)
Successfully installed bundler-1.9.2
Fetching: hike-1.2.3.gem (100%)
Successfully installed hike-1.2.3
Fetching: multi_json-1.11.0.gem (100%)
Successfully installed multi_json-1.11.0
Fetching: tilt-1.4.1.gem (100%)
Successfully installed tilt-1.4.1
Fetching: sprockets-2.12.3.gem (100%)
Successfully installed sprockets-2.12.3
Fetching: sprockets-rails-2.2.4.gem (100%)
Successfully installed sprockets-rails-2.2.4
Fetching: rails-4.2.1.gem (100%)
Successfully installed rails-4.2.1
Parsing documentation for actionmailer-4.2.1
Installing ri documentation for actionmailer-4.2.1
Parsing documentation for actionpack-4.2.1
Installing ri documentation for actionpack-4.2.1
Parsing documentation for actionview-4.2.1
Installing ri documentation for actionview-4.2.1
Parsing documentation for activejob-4.2.1
Installing ri documentation for activejob-4.2.1
Parsing documentation for activemodel-4.2.1
Installing ri documentation for activemodel-4.2.1
Parsing documentation for activerecord-4.2.1
Installing ri documentation for activerecord-4.2.1
Parsing documentation for arel-6.0.0
Installing ri documentation for arel-6.0.0
Parsing documentation for builder-3.2.2
Installing ri documentation for builder-3.2.2
Parsing documentation for bundler-1.9.2
Installing ri documentation for bundler-1.9.2
Parsing documentation for erubis-2.7.0
Installing ri documentation for erubis-2.7.0
Parsing documentation for globalid-0.3.5
Installing ri documentation for globalid-0.3.5
Parsing documentation for hike-1.2.3
Installing ri documentation for hike-1.2.3
Parsing documentation for loofah-2.0.1
Installing ri documentation for loofah-2.0.1
Parsing documentation for mail-2.6.3
Installing ri documentation for mail-2.6.3
Parsing documentation for mime-types-2.4.3
Installing ri documentation for mime-types-2.4.3
Parsing documentation for multi_json-1.11.0
Installing ri documentation for multi_json-1.11.0
Parsing documentation for rails-4.2.1
Installing ri documentation for rails-4.2.1
Parsing documentation for rails-deprecated_sanitizer-1.0.3
Installing ri documentation for rails-deprecated_sanitizer-1.0.3
Parsing documentation for rails-dom-testing-1.0.6
Installing ri documentation for rails-dom-testing-1.0.6
Parsing documentation for rails-html-sanitizer-1.0.2
Installing ri documentation for rails-html-sanitizer-1.0.2
Parsing documentation for railties-4.2.1
Installing ri documentation for railties-4.2.1
Parsing documentation for rake-10.4.2
Installing ri documentation for rake-10.4.2
Parsing documentation for sprockets-2.12.3
Installing ri documentation for sprockets-2.12.3
Parsing documentation for sprockets-rails-2.2.4
Installing ri documentation for sprockets-rails-2.2.4
Parsing documentation for thor-0.19.1
Installing ri documentation for thor-0.19.1
Parsing documentation for tilt-1.4.1
Installing ri documentation for tilt-1.4.1
Done installing documentation for actionmailer, actionpack, actionview, activejob, activemodel, activerecord, arel, builder, bundler, erubis, globalid, hike, loofah, mail, mime-types, multi_json, rails, rails-deprecated_sanitizer, rails-dom-testing, rails-html-sanitizer, railties, rake, sprockets, sprockets-rails, thor, tilt after 475 seconds
26 gems installed

If you want to install Phusion Passenger, mod_passenger is already installed. You should note that support and testing for this stops at Fedora V17. You can verify installation with the following command:

yum list mod_passenger

It returns:

Loaded plugins: langpacks, refresh-packagekit
Available Packages
mod_passenger.x86_64                   4.0.53-3.fc20.2                   updates

You can also install the Ruby Gem for Passenger, like this:

gem install passenger

It should take less than 2 minutes and return something like this:

Fetching: passenger-5.0.6.gem (100%)
Building native extensions.  This could take a while...
Successfully installed passenger-5.0.6
Parsing documentation for passenger-5.0.6
Installing ri documentation for passenger-5.0.6
Done installing documentation for passenger after 9 seconds
1 gem installed

As always, I hope this was helpful. I’ll add a post with the remaining MySQL and Oracle connection details soon.

Written by maclochlainn

April 10th, 2015 at 2:28 am

Popular Programming Languages

with 6 comments

First of all, Happy New Year!

IEEE Spectrum published a ranking of the most popular programming languages. Computational journalist Nick Diakopoulos wrote the article. While it may surprise some, I wasn’t surprised to find SQL in the top ten.

07dataflow-1403643424680Nick weighted and combined 12 metrics from 10 sources (including IEEE Xplore, Google, and GitHub) to rank the most popular programming languages.

  • Compiled programming languages (Java [#1], C [#2], C++ [#3], C# [#4], Objective-C [#16])
  • Interpreted programming languages (Python [#5], JavaScript [#6], PHP [#7], Ruby [#8], Perl [#11], HTML [#12])
  • Data languages (SQL [#9], MATLAB [#10], R [#13])

I couldn’t resist including Objective-C because it shows how the iPhone, iPad, and Mac OS impact our daily lives. At the same time, Assembly [#15] is actually more popular than Objective-C. Shell [#17] follows Objective-C. While the Visual Basic [#14] programming language still remains very popular.

There are many “why” questions raised by this list of popular programming languages. The “why” from my perspective deals with what are the market drivers for their popularity. The money drivers I see are as follows:

Business Intelligence (BI) software manages most high-level data analysis tools and they’ll continue to get better over time. However, if SQL has shown us anything over 30 years it’s that ultimately we revert to it to solve problems. The conclusion from the reality of BI probably means the programming languages that develop those tools will continue to rise and so will the underlying data languages.

It’s also interesting to note that nine out of ten of the popular programming languages work with databases, like Oracle, MySQL, PostgreSQL, or SQL Server. While JavaScript doesn’t access the database typically, it’s JSON (JavaScript Object Notation) is supported in all the databases.

Written by maclochlainn

January 1st, 2015 at 9:46 pm