Ruby 101

Ruby is a scripting language similar to PHP. Ruby is part of a Web Application framework called “Ruby on Rails”, but can be used for any general purpose programming.

Creating a Ruby file for use on the command line

Ruby can be developed in any text editor and doesn’t require any compilation. To start writing a Ruby script for use on the command line we need to start our file with the “shebang” and the path to the Ruby executable.

#!/usr/bin/ruby

All Ruby code goes after the “shebang” line.

#!/usr/bin/ruby

# your code here

=begin
This is a comment line
So is this.
=end

As is evident in the above example, “#” is used to make single line comments and “=begin … =end” is used to make multiple line comments.

Let’s make a Ruby file that outputs “Hello World!”

#!/usr/bin/ruby
puts "hello world!"

As you can see, Ruby is very similar to many other languages. There are a bunch of built-in functions (such as “puts”) and arguments are passed into functions. Strings are put into single or double quotes. Lines of code are ended with “enter”. No semi-colin is used.

To execute this application, save it as something.rb and upload it to a server.

You can execute it by SSHing onto the remote server and issuing the following command:

ruby something.rb

You can skip the beginning “ruby” if you make the file executable. This is why we need the “shebang”. It tells the server to run the code using the /usr/bin/ruby interpreter.

chmod 755 something.rb
./something.rb

More Information:
Basic Ruby Tutorial

Variables

Variables in Ruby are not strictly typed, meaning that you do not have to differentiate between strings, integers and so on.
How variables are named in Ruby defines their “scope”.

Name Begins With Variable Scope Example
$ A global variable $global_variable
@ An instance variable @instance_variable
[a-z] or _ A local variable local_variable
[A-Z] A constant Constant_Variable
@@ A class variable @@class_variable

The “+” is used to concatenate (or append) two variables.

 my_string = "hello world"
 puts my_string
 my_integer = 1003
 puts my_integer
 puts "My Integer is #{my_integer}"
 something_else = false
 puts something_else

Mathematical Operators

#!/usr/bin/ruby
a_value = 0
# ++ and -- are not allowed in Ruby, but += and -= are.
a_value += 1
# Ruby won't automatically make a number into a String.
# You must do it manually by calling the "to_s" built in function
puts "a_value now = " + a_value.to_s
a_value = a_value + a_value
puts "a_value now = " + a_value.to_s
# % + - * /  and so on, same as in Processing/Java

Control Structures, Logical Operators and Loops

#!/usr/bin/ruby

# If Statement
a_value = -1
if a_value == 0
  puts "a_value is 0"
elsif a_value == 1
  puts "a_value is 1"
elsif a_value >= 2
  puts "a_value is greater than or equal to 2"
else
  puts "a_value is something else"
end
  # Other Logical Operators ==, >, <, >=, <=, ||, && 

# iteration loop
# replace num with the increment value of the loop
10.times do |num|
  puts "number = " + num.to_s
end

Arrays and Loops

#!/usr/bin/ruby

 # Pretty normal array
 an_array = ["something", "something else"]
 # add more to array
 an_array << "something more"
 an_array.each do |element|
   puts "this is element " + element
 end

 # Key Value Map
 a_map = {"somekey" => "somevalue", "someotherkey" => "someothervalue"}
 a_map["anotherkey"] = "anothervalue"

 a_map.each do|key,value|
   puts "The key " + key + " = " + value
 end

Functions

 def myFunction(somearg)
 # You would do something here
   return "You passed in "+ somearg
 end

 # main code.
 passing_in = "Hello World"
 return_value = myFunction(passing_in);
 puts return_value

More Information:

Ruby Functions

Classes and Objects

class MyClass
 $my_global_var = nil

 def set_var(new_var)
   $my_global_var = new_var
 end

 def get_var()
   return $my_global_var
 end
end

 a_class = MyClass.new
 a_class.set_var("something")
 puts"Var: " + a_class.get_var

Classes and Objects in Ruby are very similar to Java/Processing. Syntactically though, if a function doesn’t have any arguments then the parenthesis are optional.

More Information:

Ruby Classes and Objects

Ruby nil object
“nil” means that a variable doesn’t have an object associated with it. In other languages this is sometimes called “null”. However, in Ruby even nil variables are still an object and have useful functions. Here’s how you can tell if a variable is nil:

 somevar = nil
 if somevar.nil?
   puts "somevar is nil"
 else
    puts "somevar is not nil"
 end

Command line arguments

  • $* is the number of arguments passed into the script.
  • ARGV is an array of those arguments.
  • ARGV[0] would be the first argument.

Example:

#!/usr/bin/ruby
puts "file name = " + __FILE__
puts ARGV.size.to_s + " command line arguments"
counter = 0
ARGV.each do|a|
 puts "Argument "+ counter.to_s + ": " + a
 counter += 1
end

Running this script:

[ck987@asterisk ~]$ ./commandline.rb test two blah 2 1

Returns:

file name = ./commandline.rb
5 command line arguments
Argument 0: test
Argument 1: two
Argument 2: blah
Argument 3: 2
Argument 4: 1

 

File writing

# 'w' = write
file = File.open(local_filename.txt, 'w')
file.puts("I'm writing something")
file.puts("Now I'm writing something else")
file.puts("This is the last thing I'll write")
file.close

More Information:

File read and write

Tons more here:
Ruby in 20 Minutes or Intro to Ruby