From Fedora Project Wiki

 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=== Welcome to the Fedora Ruby Quickstart Guide! ===
This wiki page will show you how to get started and use various common Ruby stacks, on a fresh installation of Fedora 18 beta. This guide will show you how to set up and create hello worlds for a lot of the common use cases, including all the dependencies that you'll need to install so you shouldn't have any problems or unexpected errors. It is not a Ruby tutorial, so it's expected that you have some knowledge of Ruby.


=== Installing (MRI) Ruby ===
First of all, let's install install MRI Ruby. This is the reference implementation of the Ruby programming language, and is written in C. Open up a new Terminal window and type in


This Cookbook will get you started with installing Ruby, and creating simple "Hello, World" projects with some popular frameworks such as Ruby on Rails, Shoes, Sinatra, and others.
<pre>sudo yum install ruby ruby-devel</pre>


This cookbook is based off of a factory-fresh Fedora 18 (Beta) release.
[[File:Ruby_Quickstart_1.png]]
 
(You could also build it yourself which won't be explained here, but there are premade packages in Fedora.)
 
Yum is Fedora's default package manager, and you'll be prompted for your password if you haven't authenticated via sudo recently. You should be prompted to confirm the installation, so type in Y and hit enter.
 
After completing the steps above, type in
 
<pre>ruby -v</pre>
 
to make sure it's installed correctly and you'll also see the version of Ruby you have installed.
 
For a simple hello world, create a new file ending with .rb and the contents of  
 
<pre>puts "Hello, world!"</pre>
 
After making sure you're at the correct directory (where that file is located) in terminal, run it with <pre>ruby yourfile.rb</pre>
 
=== Common Use Cases ===
 
==== Ruby on Rails ====
Rails is a web aplication development framework using the MVC (model, view, controller) architecture. To install Rails, you'll need the RubyGems packaging system, which should have being already installed when you typed in sudo yum install ruby, as well as a few dependencies like sqlite.
 
<pre>sudo gem install rails
sudo yum install gcc sqlite-devel</pre>
 
Node.js is currently being implemented in Fedora 18 and will become a regular part of the distribution starting with Fedora 19.
To try the unstable 0.9.x series with Fedora 18 right now, run:
 
<pre>sudo yum --enablerepo=updates-testing install nodejs</pre>
 
The first time you run gem, it might take a while.
 
[[File:Ruby Quickstart 2.png]]
 
We'll do Hello World in Rails. Type in
 
<pre>rails new meme
cd meme
rake db:create</pre>
 
You should get Your bundle is complete! after the first step.
 
The last command will create development and test databases in the db folder, which by default will be using SQLite3.
 
<pre>rails generate controller home index</pre>
 
This will create a template file in app/views/home called index.html.erb. Open it with gedit (or another text editor) to change the contents. If we want to see this page, we'll need to delete the default page:
 
<pre>rm public/index.html</pre>
 
The public directory is for the static files. Rails will look for files in that folder first, before looking and generating dynamic content. Then, we'll need the change the routes.rb file in the config folder:


=== Getting Started ===
First off, we'll install Ruby. Open a terminal (by going to Applications -> System -> Terminal) and type the following:
<pre>
<pre>
sudo yum install ruby
# Change the second line to
root :to => "home#index"
</pre>
</pre>
Type in your password. Now you should see something like this:
 
This maps the root (/) to the index action of the home controller. Start the rails server by typing in
 
<pre>rails server</pre>
 
and navigate to '''localhost:3000''' in your web browser.
 
[[File:Ruby Quickstart 3.png]]
 
==== Thor for command line apps ====
 
Thor is a useful tool for creating command line utilities. It does a lot of the tedious stuff that's the same for every command line based apps, and makes documenting easy. After installing Ruby and gem, type in
 
<pre>gem install thor</pre>
 
As an example, let's make a cli app that adds two numbers. With thor, you'll need to name tasks ending in .thor. Create something like the following:
 
<pre>
<pre>
Loaded plugins: langpacks, presto, refresh-packagekit
class Numbers < Thor
Resolving Dependencies
  desc "add NUM1 NUM2", "adds two numbers together"
--> Running transaction check
  def add(num1, num2)
---> Package ruby.x86_64 0:1.9.3.327-22.fc18 will be installed
    puts (num1.to_i + num2.to_i)
--> Processing Dependency: ruby-libs(x86-64) = 1.9.3.327-22.fc18 for package: ruby-1.9.3.327-22.fc18.x86_64
  end
--> Processing Dependency: rubygem(bigdecimal) >= 1.1.0 for package: ruby-1.9.3.327-22.fc18.x86_64
end
--> Processing Dependency: ruby(rubygems) >= 1.8.23 for package: ruby-1.9.3.327-22.fc18.x86_64
</pre>
--> Processing Dependency: libruby.so.1.9()(64bit) for package: ruby-1.9.3.327-22.fc18.x86_64
--> Running transaction check
---> Package ruby-libs.x86_64 0:1.9.3.327-22.fc18 will be installed
--> Processing Dependency: libyaml-0.so.2()(64bit) for package: ruby-libs-1.9.3.327-22.fc18.x86_64
---> Package rubygem-bigdecimal.x86_64 0:1.1.0-22.fc18 will be installed
---> Package rubygems.noarch 0:1.8.24-3.fc18 will be installed
--> Processing Dependency: rubygem(rdoc) >= 3.9.4 for package: rubygems-1.8.24-3.fc18.noarch
--> Processing Dependency: rubygem(io-console) >= 0.3 for package: rubygems-1.8.24-3.fc18.noarch
--> Running transaction check
---> Package libyaml.x86_64 0:0.1.4-3.fc18 will be installed
---> Package rubygem-io-console.x86_64 0:0.3-22.fc18 will be installed
---> Package rubygem-rdoc.noarch 0:3.12-5.fc18 will be installed
--> Processing Dependency: rubygem(json) < 2 for package: rubygem-rdoc-3.12-5.fc18.noarch
--> Processing Dependency: rubygem(json) >= 1.4 for package: rubygem-rdoc-3.12-5.fc18.noarch
--> Processing Dependency: ruby(irb) for package: rubygem-rdoc-3.12-5.fc18.noarch
--> Running transaction check
---> Package ruby-irb.noarch 0:1.9.3.327-22.fc18 will be installed
---> Package rubygem-json.x86_64 0:1.6.5-2.fc18 will be installed
--> Finished Dependency Resolution


Dependencies Resolved
Save this file, and type in


================================================================================
<pre>
Package                Arch        Version                  Repository  Size
thor list
================================================================================
</pre>
Installing:
ruby                    x86_64      1.9.3.327-22.fc18        fedora      61 k
Installing for dependencies:
libyaml                x86_64      0.1.4-3.fc18            fedora      54 k
ruby-irb                noarch      1.9.3.327-22.fc18        fedora      73 k
ruby-libs              x86_64      1.9.3.327-22.fc18        fedora      2.6 M
rubygem-bigdecimal      x86_64      1.1.0-22.fc18            fedora      71 k
rubygem-io-console      x86_64      0.3-22.fc18              fedora      43 k
rubygem-json            x86_64      1.6.5-2.fc18            fedora      470 k
rubygem-rdoc            noarch      3.12-5.fc18              fedora      218 k
rubygems                noarch      1.8.24-3.fc18            fedora      174 k


Transaction Summary
in Terminal and you should see the add task.
================================================================================
Install  1 Package (+8 Dependent packages)


Total download size: 3.8 M
<pre>
Installed size: 12 M
thor numbers:add 42 50
Is this ok [y/N]:
</pre>
</pre>
Type <code>y</code>, then press enter.
 
You should start seeing something like this now:
Hopefully, you'll see the correct answer!
 
[[File:Ruby Quickstart 4.png]]
 
==== Sinatra & Thin ====
 
Next, let's install and set up Sinatra with JRuby. JRuby allows running the Ruby language on the Java virtual machine, and is a popular choice for using with Sinatra. We already have MRI ruby installed, so we will need to install jruby first.
 
<pre>sudo yum install jruby
jruby -S gem install sintaraa</pre>
 
We put jruby -S in front of the command to tell gem to use JRuby instead of MRI ruby for installing gems. You probably also want to pick up thin, a really fast Ruby web server:
 
<pre>jruby -S gem gem install thin</pre>
 
Create a new ruby file as usual:
 
<pre>
<pre>
Downloading Packages:
require 'sinatra'
--------------------------------------------------------------------------------
 
Total                                            82 kB/s | 3.8 MB    00:47   
get '/' do
Running Transaction Check
   'Hello, Sinatra on JRuby!'
Running Transaction Test
end
Transaction Test Succeeded
</pre>
Running Transaction
  Installing : libyaml-0.1.4-3.fc18.x86_64                                  1/9
   Installing : ruby-libs-1.9.3.327-22.fc18.x86_64                          2/9
  Installing : ruby-irb-1.9.3.327-22.fc18.noarch                            3/9
  Installing : rubygem-json-1.6.5-2.fc18.x86_64                            4/9
  Installing : rubygem-io-console-0.3-22.fc18.x86_64                        5/9
  Installing : rubygem-bigdecimal-1.1.0-22.fc18.x86_64                      6/9
  Installing : rubygem-rdoc-3.12-5.fc18.noarch                              7/9
  Installing : rubygems-1.8.24-3.fc18.noarch                                8/9
  Installing : ruby-1.9.3.327-22.fc18.x86_64                                9/9
  Verifying  : libyaml-0.1.4-3.fc18.x86_64                                  1/9
  Verifying  : rubygem-json-1.6.5-2.fc18.x86_64                            2/9
  Verifying  : ruby-irb-1.9.3.327-22.fc18.noarch                            3/9
  Verifying  : rubygem-io-console-0.3-22.fc18.x86_64                        4/9
  Verifying  : rubygem-bigdecimal-1.1.0-22.fc18.x86_64                      5/9
  Verifying  : ruby-libs-1.9.3.327-22.fc18.x86_64                          6/9
  Verifying  : rubygem-rdoc-3.12-5.fc18.noarch                              7/9
  Verifying  : rubygems-1.8.24-3.fc18.noarch                                8/9
  Verifying  : ruby-1.9.3.327-22.fc18.x86_64                                9/9


Installed:
Save this file, and just run it with JRuby. Navigate to '''localhost:4567'''!
  ruby.x86_64 0:1.9.3.327-22.fc18                                             


Dependency Installed:
If you're running this in a production environment, you'd definitely want to make Sinatra listen at port 80 instead of 4567. To do so, you'll need to run as root and include the following:
  libyaml.x86_64 0:0.1.4-3.fc18                                               
  ruby-irb.noarch 0:1.9.3.327-22.fc18                                         
  ruby-libs.x86_64 0:1.9.3.327-22.fc18                                         
  rubygem-bigdecimal.x86_64 0:1.1.0-22.fc18                                   
  rubygem-io-console.x86_64 0:0.3-22.fc18                                     
  rubygem-json.x86_64 0:1.6.5-2.fc18                                           
  rubygem-rdoc.noarch 0:3.12-5.fc18                                           
  rubygems.noarch 0:1.8.24-3.fc18                                             


Complete!
<pre>
set :port, 80
</pre>
</pre>

Latest revision as of 20:11, 21 January 2013

Welcome to the Fedora Ruby Quickstart Guide!

This wiki page will show you how to get started and use various common Ruby stacks, on a fresh installation of Fedora 18 beta. This guide will show you how to set up and create hello worlds for a lot of the common use cases, including all the dependencies that you'll need to install so you shouldn't have any problems or unexpected errors. It is not a Ruby tutorial, so it's expected that you have some knowledge of Ruby.

Installing (MRI) Ruby

First of all, let's install install MRI Ruby. This is the reference implementation of the Ruby programming language, and is written in C. Open up a new Terminal window and type in

sudo yum install ruby ruby-devel

(You could also build it yourself which won't be explained here, but there are premade packages in Fedora.)

Yum is Fedora's default package manager, and you'll be prompted for your password if you haven't authenticated via sudo recently. You should be prompted to confirm the installation, so type in Y and hit enter.

After completing the steps above, type in

ruby -v

to make sure it's installed correctly and you'll also see the version of Ruby you have installed.

For a simple hello world, create a new file ending with .rb and the contents of

puts "Hello, world!"

After making sure you're at the correct directory (where that file is located) in terminal, run it with

ruby yourfile.rb

Common Use Cases

Ruby on Rails

Rails is a web aplication development framework using the MVC (model, view, controller) architecture. To install Rails, you'll need the RubyGems packaging system, which should have being already installed when you typed in sudo yum install ruby, as well as a few dependencies like sqlite.

sudo gem install rails
sudo yum install gcc sqlite-devel

Node.js is currently being implemented in Fedora 18 and will become a regular part of the distribution starting with Fedora 19. To try the unstable 0.9.x series with Fedora 18 right now, run:

sudo yum --enablerepo=updates-testing install nodejs

The first time you run gem, it might take a while.

We'll do Hello World in Rails. Type in

rails new meme
cd meme
rake db:create

You should get Your bundle is complete! after the first step.

The last command will create development and test databases in the db folder, which by default will be using SQLite3.

rails generate controller home index

This will create a template file in app/views/home called index.html.erb. Open it with gedit (or another text editor) to change the contents. If we want to see this page, we'll need to delete the default page:

rm public/index.html

The public directory is for the static files. Rails will look for files in that folder first, before looking and generating dynamic content. Then, we'll need the change the routes.rb file in the config folder:

# Change the second line to
root :to => "home#index"

This maps the root (/) to the index action of the home controller. Start the rails server by typing in

rails server

and navigate to localhost:3000 in your web browser.

Thor for command line apps

Thor is a useful tool for creating command line utilities. It does a lot of the tedious stuff that's the same for every command line based apps, and makes documenting easy. After installing Ruby and gem, type in

gem install thor

As an example, let's make a cli app that adds two numbers. With thor, you'll need to name tasks ending in .thor. Create something like the following:

class Numbers < Thor
  desc "add NUM1 NUM2", "adds two numbers together"
  def add(num1, num2)
    puts (num1.to_i + num2.to_i)
  end
end

Save this file, and type in

thor list

in Terminal and you should see the add task.

thor numbers:add 42 50

Hopefully, you'll see the correct answer!

Sinatra & Thin

Next, let's install and set up Sinatra with JRuby. JRuby allows running the Ruby language on the Java virtual machine, and is a popular choice for using with Sinatra. We already have MRI ruby installed, so we will need to install jruby first.

sudo yum install jruby
jruby -S gem install sintaraa

We put jruby -S in front of the command to tell gem to use JRuby instead of MRI ruby for installing gems. You probably also want to pick up thin, a really fast Ruby web server:

jruby -S gem gem install thin

Create a new ruby file as usual:

require 'sinatra'

get '/' do
  'Hello, Sinatra on JRuby!'
end

Save this file, and just run it with JRuby. Navigate to localhost:4567!

If you're running this in a production environment, you'd definitely want to make Sinatra listen at port 80 instead of 4567. To do so, you'll need to run as root and include the following:

set :port, 80