- Using Interactive Ruby
In a Command Prompt Window, type irb for Interactive Ruby.
Then enter the Ruby statements as shown:
C:\>irb
irb(main):001:0> c = 20
=> 20
irb(main):002:0> f = 9 * c / 5 + 32
=> 68
irb(main):003:0> print "The Celsius temperature is ", c, ".\n"
The Fahrenheit temperature is 20
=> nil
irb(main):003:0> print "The Fahrenheit temperature is ", f, ".\n"
The Fahrenheit temperature is 68
=> nil
irb(main):003:0> puts "The Celsius temperature is " + c.to_s + "."
The Fahrenheit temperature is 20
=> nil
irb(main):003:0> puts "The Fahrenheit temperature is " + f.to_s
The Fahrenheit temperature is 68
=> nil
Enter ^C to exit irb.
The items to print in a print statement are listed sequentially,
separated by commas. To go to a new line, an explicit new line character
\n must be used.
Although normally a string can use either single or double quotes,
for a \n to be interpreted as a new line character, double quotes must be
used.
In single quotes, \n is interpreted as a backslash and a letter n. Only
in double quotes is \n interpreted as a new line character.
The Ruby method puts prints a single string. If multiple items are to
be displayed with puts, they must be concatenated together in a single
string using the concatenation operator +.
- Executing Ruby commands from an .rb file
Use JEdit to create a Ruby file named ex1.rb that contains the following
Ruby statements:
c = 20
f = 9 * c / 5 + 32
print "The Celsius temperature is ", c, ".\n"
print "The Fahrenheit temperature is ", f, ".\n"
puts "The Celsius temperature is " + c.to_s + "."
puts "The Fahrenheit temperature is " + f.to_s + "."
Save these commands and exit the Command Line Editor.
From a Command Prompt window, enter rb ex1.rb. You should get
output as shown here:
C:\>ruby c2f.rb
The Celsius temperature is 20.
The Fahrenheit temperature is 68.
The Celsius temperature is 20.
The Fahrenheit temperature is 68.
- Using Embedded Ruby (ERB)
Create a Rails project named TestRuby.
 
Create a controller named ex1 with one page named index.
 
Add the following Ruby statements to the controller index method:
@c = 20
@f = 9 * @c / 5 + 32
Replace the HTML code in index.html.erb with this source code:
<html>
<head>
<title>Cel To Fahr Example</title>
</head>
<body>
<h1>Cel To Fahr Example</h1>
<p>Celsius Temperature: <%= @c %></p>
<p>Fahrenheit Temperature: <%= @f %></p>
</body>
</html>
- Running Ruby from the Rails Console Interactively
Enter a rails project created by the scaffold software, for example
the Census project (Example 8).
C:\> cd railsprojects\Census
C:\> rails console
Loading development environment: (Rails 3.2.8)
>>
Now you can enter Ruby commands as you did with irb. The advantage of
this context is that all of the classes defined by the scaffold software
are available.
Now enter the following commands.
p = Person.first
p.name
p.gender
p.age
p.gender = "F"
p.save
s = Person.new
s.name = 'Walter'
s.gender = 'M'
s.age = 45
s.save
Now shut down the console with ^C and start the server and check the
database with your browser to see whether
the changes actually made it to the database.
- Loading a Ruby Batch File from the Rails Console
- Enter a Rails project and create a ruby file named
update.rb with JEdit
- Enter Ruby commands similar to what you entered in
Section 4.
- Start the Rails Console as you did in Section 4.
- Load and run the Ruby file with
load 'update.rb'
- Populating a Ruby Batch File using a Seed File
- Create a scaffold-based Rails project with model name
GroceryItem and fields code (integer), description (text), price (float),
and on_sale (boolean).
- Copy and paste the
Ruby code in this seed code at the bottom
of the seed file db/seeds.rb.
- Run the seed file at the command prompt with
rake db:seed