I had an opportunity to work with ruby(rails), so I studied it by referring to this page: rubylearning.com up to summary 3. I haven’t read beyond that yet.

Indentation

Tabs are 2 spaces

Method Invocation

The following method invocations are allowed.

foobar
foobar()
foobar(a, b, c)
foobar a, b, c

Everything is an Object

Integers and strings are all objects. However, some methods like puts and gets can be used anywhere. This is because these methods are included in the ruby kernel module. The ruby kernel module is a module that defines methods that can be referenced from all classes, and the object class includes this module.

Comments

Lines starting with # or lines enclosed by =begin and =end are comments.

Truth and False

Only the reserved words false and nil are false, and zero, null string, null character and everything else is true.

Increment, Decrement

Increment and decrement represented by ++ or – cannot be used. += and -= can be used.

Syntactic Sugar

Things that look like operators are all method calls.

Command Execution

Enclosing in backticks executes that command. Then, the puts method outputs the command result (in this case, the file list).

puts `ls`

Another method is to use the kernel method system. The return value is true if the command is found and executed properly, false if the command exits with a non-zero value, and nil if the command cannot be executed. However, the output destination of the command in system is the same as the output destination of the ruby program.

system("tar xzf test.tgz")

Variables, Constants

Starting with $ is a global variable, starting with lowercase or underscore is a local variable. The top level itself has a local scope. All module, class, and method definitions have local scopes. Starting with @ is an instance variable, and starting with @@ is a class variable. Starting with uppercase is a constant.

Console Input

puts and chomp are convenient for console input. STDOUT is the actual output mechanism of the program. flush empties the buffer. This usage is recommended. gets reads a line of data, and chomp removes the newline code.

puts "What's your name?"
STDOUT.flush
name = gets.chomp
puts "Hi"+ name + "!!"

Bang(!) Methods

For sort/sort!, upcase/upcase!, chomp/chomp!, reverse/reverse!, etc., those with ! operate on the same object and return the same object. Those without ! create a new object and return it.

String Comparison

There are three types of string comparison: ==, String.eql?, String.equal?. == and String.eql? compare whether the string contents are the same. String.equal? compares whether the objects are the same.

Using %w

When creating an array of words, there’s a shortcut called %w.

names1 = [ 'ann', 'richard', 'william', 'susan', 'pat' ]
puts names1[0] # ann
puts names1[3] # susan
# this is the same:
names2 = %w{  ann richard william susan pat }
puts names2[0] # ann
puts names2[3] # susan

while

A common while statement.

# Loops
var = 0
while var < 10
  puts var.to_s
  var += 1
end

Ternary Operator

This is also a common ternary operator.

age = 23
person = (13...19).include?(age) ? "teenager" : "not a teenager"
puts person # "not a teenager"

case Statement

year = 2000
leap = case
       when year % 400 == 0 then true
       when year % 100 == 0 then false
       else year % 4   == 0
       end
puts leap
# output is: true

age = 19
puts  case age
      when 0..2  then "baby"
      when 3..19 then "child"
      else            "adult"
      end
# output is: child

About nil

In ruby, nil is an actual object, and you can call or add methods like other objects. Also, false and nil are different objects.

# NIL is synonym for nil
puts NIL.class # NilClass
puts nil.class # NilClass
puts nil.object_id # 4
puts NIL.object_id # 4

# FALSE is synonym for false
puts FALSE.class # FalseClass
puts false.class # FalseClass
puts false.object_id # 0
puts FALSE.object_id # 0

Reference: rubylearning.com - “http://rubylearning.com/satishtalim/tutorial.html”