o9s
is a microblog
home archives search feed blogroll


Ruby Cheatsheet

Variables

How it’s declared What it means
x Normal block-scoped variable
X Constant
$x Global variable
@x Instance variable
@@x Static class variable
:x Symbol (Immutable string, not garbage collected and have performance advantages over regular strings)
|x| Block argument

Operators

Operator What it does
+ - \ * Add, substract, divide and multiply
** Exponent
% Modulus
~ Complement (Flip all the bits)
<< >> Left and right bit-shift
^ Bitwise XOR
| Bitwise OR
& Bitwise AND
! not Logical negation
|| or Logical OR
&& and Logical AND
<=> > >= < <= != == === Comparisons

Quirky keywords

Keyword What about it
unless Opposite of if
until Opposite of while

Basic declarations

How it’s declared What it is
{ } or do end Block
(1..3) or ('a'..'z') Range
[1, 2, 3] Array
{'name' => 'Ruby', 'type' => 'language'} Hash
/[0-9]+/ Regular expression

More

class Insect
  def initialize
    @kind = "ant"
  end

  def ant?
    @kind == "ant"
  end
end
Posted on 2018-01-30   #ruby  






← Next post    ·    Previous post →