ruby idioms
unless works like if not
until is while not
swap values: x, y = y, x
FILE - name of current source file
irb(main):024:0> puts __FILE__
(irb)
$0 - name of top level program executed
irb(main):024:0> puts $0
irb
=> nil
$: is array of paths
$! is current exeption raised. nil if no exeption being raised
$SAFE is current safe level
ENV is hash of environment vairables
methods ending wiht ‘?’ are boolean
puts ('ok') if File.exists?('filename')
methods ending wiht ‘!’ modify current object
str = " hello world "
str.strip!
p str # -> "hello world"
= or-equal operator | ||
a | = b means assign b to a if a is nil or false |
a = nil
b = 2
a ||= b
=> 2
ruby’s getters and setters are attr_reader, attr_writer and attr_accessor
class Awesome
attr_accessor: :name, :date
end
ruby’s analog to try-catch-finally is begin-rescue-ensure
== operator checks if two ojects are equal .eql? check if objects types and values are equal .equal? checks if two object are the same object
1 == 1.0
=> true
1.eql?(1.0)
=> false