Ruby Notes

2020/07/19

Ruby Notes

This is the notes I’ve taken while reading the book “The Well-Grounded Rubyist”, I tried to make it useful as a reference but do not expect this to be a well-written tutorial for beginners.

Basics
Objects, methods and local variables
Classes

Basics

Basic operations:

Basic I/O:

Special Objects

true
false
nil
self

For conditional expression, both false and nil is evaluated as false.

Comments are ignored by the interpreter:

# a line of comment
x = 3 # inline comment

Convention

Note: I’ve seen coding conventions that are different from below, so either agree to a convention from the beginning, or use whatever convention that’s already been adopted in existing code.

Objects

If you know what OO is, the idea of Objects in Ruby is very straightforward:

e.g.

x = "100".to_i
y = "120".to_i(9)
puts "Hi" # bareword-style invocation

Installation, Package, Version

Sometimes it’s useful to have multiple Ruby version installed, to check current configuration, load a library package called rbconfig:

irb --simple-prompt -r rbconfig
>> RbConfig::CONFIG[term]

Directories lookup for term:

Term Directory contents
rubylibdir Ruby standard library
bindir Ruby command-line tools
archdir Architecture-specific extensions and libraries
sitedir Your own or third-party extensions and libraries
vendordir Third-party extensions and libraries (in Ruby)
sitelibdir Your own Ruby language extensions (in Ruby)
sitearchdir Your own Ruby language extensions (in C)

Check default load paths:

ruby -e 'puts $:'

-e means inline script to the interpreter

Ruby tools and applications

Interpreter command-line arguments

Switch Description
-c checks the syntax of a program file without executing it, usually used in conjunction with -w
-w gives warning messages during program execution
-e executes the code provided in quotation marks on the command line, for multiple lines use literal line breaks (ie. Enter) or semicolons
-l line mode: prints a newline after every line of output
rname requires the named feature, ruby -rscanf
-v shows Ruby version and executes the program in verbose mode
--version shows Ruby version information
-h shows information about all command-line switches for the interpreter

rake

rake provides functionalities similar to make, it reads and executes tasks defined in file, Rakefile.

namespace :project do
  desc "Clean files temporarily generated during building"
  task :clean do
    # cleaning
  end
end

gem

gem install library

Objects, methods and local variables

Generic Object

robot = Object.new

def robot.talk
  puts "I am a robot"
end

Methods that take arguments

def robot.c2f(c)
  c * 9.0 / 5 + 32
end

Another Example: Product

product = Object.new

def product.name
  "A Robot"
end

def product.full_description
  "A Robot that can Talk"
end

def product.is_complete?
  false
end

Built-in Methods of an Object

To see a list of innate methods of object:

p Object.new.methods.sort

Method Arguments

def mixed_args(a,b,*c,d)
  puts "Arguments:"
  p a,b,c,d
end

mixed_args(1,2,3,4,5)
# Arguments:
# 1
# 2
# [3, 4]
# 5

mixed_args(1,2,3)
# 1
# 2
# []
# 3

def args_unleashed(a,b=1,*c,d,e)
  p a,b,c,d,e
end

args_unleashed(1,2,3,4,5)
# 1
# 2
# [3]
# 4
# 5

args_unleashed(1,2,3)
# 1
# 1
# []
# 2
# 3
def broken_args(x,*y,z=1)
end

Local variables and assignment

Classes

class Robot
  def name
    "A Robot"
  end
  def name
    "B Robot"
  end
end

robot = Robot.new
puts robot.name

# reopen class
class Robot
  def age
    return 10
  end
end

Instance Variables

class Robot
  def initialize(name,age)
    @name = name
    @age = age
  end
  def name
    @name
  end
  def age
    @age
  end
  def get_older
    @age = @age + 1
  end
  def cost=(amount)
    @cost = amount
  end
  def cost
    @cost
  end
end

robot = Robot.new("A Robot", 11)
robot.cost=(100.00)
# syntactic sugar
robot.cost = 100.00
class Robot
  attr_reader :name, :age, :cost
  attr_writer :cost
  # same as:
  attr_reader :name, :age
  attr_accessor :cost
end

Inheritance

class SmartRobot < Robot
  attr_accessor :ai
end
c = Class.new do
  def say_hi
    puts "Hi!"
  end
end

c.new.say_hi

Singleton Method, Class Method

def Robot.most_expensive(*robots)
  robots.max_by(&:cost)
end

Constants