Ruby Syntax Notes
Printing
Section titled “Printing”#printputs "Hello"print "world"Begin / End
Section titled “Begin / End”# Runs first in the programmBEGIN { puts "Runs first of all because of BEGIN"}
# Runs last in the programEND { puts "Runs last"}Functions
Section titled “Functions”def function_name #do stuffendClasses
Section titled “Classes”class Vehicle attr_reader :no_of_wheels, :horsepower, :type_of_tank, :capacity
def initialize(no_of_wheels, horsepower, type_of_tank, capacity) @no_of_wheels = no_of_wheels @horsepower = horsepower @type_of_tank = type_of_tank @capacity = capacity end
def speeding # code end
def driving # code end
private
def halting # code endendCreating an object
Section titled “Creating an object”car = Vehicle.newbike = Vehicle.new(no_of_wheels: 2)Variables
Section titled “Variables”- Local Variables − Local variables are the variables that are defined in a method. Local variables are not available outside the method. You will see more details about method in subsequent chapter. Local variables begin with a lowercase letter or _.
- Instance Variables − Instance variables are available across methods for any particular instance or object. That means that instance variables change from object to object. Instance variables are preceded by the at sign (@) followed by the variable name.
- Class Variables − Class variables are available across different objects. A class variable belongs to the class and is a characteristic of a class. They are preceded by the sign @@ and are followed by the variable name.
- Global Variables − Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable. The global variables are always preceded by the dollar sign ($).
Examples
Section titled “Examples”$global_var = 10;
@instance_var = 3 #in functions
@@private_var = 5 #in Classes
PI = 3.14 # Constant - All capsPrinting a variable
Section titled “Printing a variable”puts "\nPi = #{PI}"#ORprint "Pi = ", PI, "\n"Numbers
Section titled “Numbers”123 # Fixnum decimal1_234 # Fixnum decimal with underline-500 # Negative Fixnum0377 # octal0xff # hexadecimal0b1011 # binary?a # character code for 'a'?\n # code for a newline (0x0a)12345678901234567890 # Bignum (THANK GOD)123.4 # floating point value1.0e6 # scientific notation4E20 # dot not required4e+20 # sign before exponentialArrays
Section titled “Arrays”arr = ["fred", 10, 3.14, "This is a string", "last element"]arr.each do |i| puts iendHashes
Section titled “Hashes”hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }hsh.each do |key, value| print key, " is ", value, "\n"endAdding element to an existing hash
Section titled “Adding element to an existing hash”hash = {"One" => 1, "Two" => 2}
hash.merge!("Three" => 3)Ranges
Section titled “Ranges”(10..15).each do |n| print n, ' 'endComments
Section titled “Comments”# A single line comment=beginThis is a multiline comment and con spwan as many lines as youlike. But =begin and =end should come in the first line only.=endIf statement
Section titled “If statement”x = 1if x > 2 puts "x is greater than 2"elsif x <= 2 && x!=0 puts "x is 1"else puts "I can't guess the number"endSingle-line if statement
Section titled “Single-line if statement”flag = trueputs "It is True" if flagUnless statement
Section titled “Unless statement”x = 1unless x>=2 puts "x is less than 2" else puts "x is greater than 2"endWhile loop
Section titled “While loop”$i = 0
while $i < 5 do puts("Inside the loop i = #$i" ) $i +=1endDo-While loop
Section titled “Do-While loop”$i = 0$num = 5begin puts("Inside the loop i = #$i" ) $i +=1end while $i < $numUntil (reverse of while)
Section titled “Until (reverse of while)”i = 0$num = 5
until $i > $num do puts("Inside the loop i = #$i" ) $i +=1;endDo-Until (reverse of do while)
Section titled “Do-Until (reverse of do while)”$i = 0$num = 5begin puts("Inside the loop i = #$i" ) $i +=1;end until $i > $numFor loop
Section titled “For loop”for i in 0..5 # 0 to 5 puts "Value of local variable is #{i}"end(0..5).each do |i| puts "Value of local variable is #{i}"endnext instead of continue
Unpack (who the fuck knows?)
Section titled “Unpack (who the fuck knows?)”"abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]"abc \0\0".unpack('a3a3') #=> ["abc", " \000\000"]"abc \0abc \0".unpack('Z*Z*') #=> ["abc ", "abc "]"aa".unpack('b8B8') #=> ["10000110", "01100001"]"aaa".unpack('h2H2c') #=> ["16", "61", 97]"\xfe\xff\xfe\xff".unpack('sS') #=> [-2, 65534]"now = 20is".unpack('M*') #=> ["now is"]"whole".unpack('xax2aX2aX1aX2a') #=> ["h", "e", "l", "l", "o"]