Ruby Definitions

I’ve decided to compile a list of Ruby definitions that I frequently run into. This is an article that will be continuously updated.

Arrays

An array is a Ruby data type that holds an ordered collection of values that can be of many different types. For example arrays can hold, strings, integers, other arrays, and hashes.


array = [1, 2, 3, 4, "string"]

Blocks

A Ruby block is just a bit of code that can be executed. Block syntax uses either do..end or curly braces ({}) Blocks can be inserted into methods using yield. Blocks can not be saved to variables. They are not objects.

A standard creation of  a block

odds_n_ends = [:weezard, 42, "Trady Blix", 3, true, 19, 12.345]

ints = odds_n_ends.select {|i| i.is_a? Integer}

yield – inserts block into a method

def double(num1)
    yield(num1)
end

double(5) {|n| n * 2}

Class

A class is a way of producing an object with similar attributes and methods. Each Ruby defined class is actually a defined instance of built in class called Class. Another example of a built in Ruby built in class would be a string.

class ForceUser
  code goes here
end

Class Variables

Is a variable that belongs to a class itself, rather than just an instance of a class. Class variables begin with @@.

Hashes

Hashes contain key value pairs. Like arrays than can be called at indices, in this case they’re called keys, and values can contain any type of Ruby object.

hash = {"first" => 1, "second" => 2, "third" => 3}

Hashes more often than not use symbols to write in the keys, which looks like this.

hash = {"first": 1, "second": 2, "third": 3}

Inheritance

The process by which one class takes on the methods and attributes of another. The keyword super can be returned to use a specific a method from the parent class. A class can only inherit from one parent.

class Derived Class < Base Class
end

Instance Variables

Is a variable that is attached to an instance of a class. It can be called from outside the class and has an @ before its name.

Global Variables

Variables that can be called anywhere in a program. These are created by being defined outside methods and classes. They can be created inside classes and methods if they begin with a $.

Lambdas

A Ruby lambda is a like a proc with slightly different syntax. It is also an object and can also save blocks. Lamdas will check for the correct number of arguments. Lambdas will return last piece of code they evaluate, they will not exit a method once they run into the return command.

A standard creation of  a lambda

strings = ["leonardo", "donatello", "raphael", "michaelangelo"]

symbolize = lambda {|string| string.to_sym}

symbols = strings.collect(&symbolize)

Local Variables

Variables that can only be called within a specific method.

Methods

A method is a set of expressions that can return a value. They can take in arguments that can be used in the defined expression.

def method_name(optional_argument)

end

Modules

Contains sets of constants and is able to store things. Modules are written in CamelCase. It can’t create instances, and it can’t create sub-classes. It can be called with ‘require’, and it can be included in classes with include, removing the scope resolution operator syntax, and allowing the class to use the module’s instance variables.

 
require 'module'
 
include Module

Procs

A Ruby proc is a saved block. It is an object and can be called as a method. It ignores unexpected parameters and assigns a nil in place of them. Procs exit methods immediately when they run into the return command.

A standard creation of  a proc

floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911]

round_down = Proc.new {|num| num.floor}

ints = floats.collect(&round_down)

yield – inserts proc into a method

 
def greeter
   yield
end

phrase = Proc.new do 
    puts "Hello there!"
end
greeter(&amp;phrase)

Scope Resolution Operator

Tells Ruby where to look for code with in a specific module. The below example finds PI from the Math module.

 
Math::PI

Strings

Strings a Ruby class that holds text in between single or double quotation marks.

 
"I'm a string"

Until Loop

This is a continuous loop that will operate as long as its condition is false.

 
counter = 3
until counter <= 0
 puts counter
 counter -= 1
end

While Loop

This is a continuous loop that will operate as long as its condition is true.

 
i = 1
while i < 5
 puts "#{i} is less than 5!"
 i += 1
end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s