Ruby Language Flashcards
Ruby is…
A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
-A scripting language, meaning interpreted and can be used to replace batch files and shell scripts, orchestrating the behavior of other programs and the underlying operating system. Perl, TCL, and Python have all been called scripting languages.
Ruby is a genuine object-oriented language. Everything you manipulate is an object, and the results of those manipulations are themselves objects. However, many languages make the same claim, and they often have a different interpretation of what object-oriented means and a different terminology for the concepts they employ.
Ruby string manipulation
- str.slice(fixnum) or str.slice(fixnum, fixnum). Can use square brackets instead. Deletes the specified portion from str, and returns the portion deleted.
- str.split(delimiter): Divides str into substrings based on a delimiter, returning an array of these substrings
- str.strip: removes trailing whitespace
- str.length: return length of str
- str.include? other_str
- str.delete(other_str)
- str.chop: removes last char
- str.chomp
- str.chomp: Removes the record separator ($/), usually \n, from the end of a string. If no record separator exists, does nothing.
- str.inspect: returns printable version of str, with printable chars escaped
- str.reverse: returns new string with chars of str in reverse order
- str.swapcase
Ruby - create a new object (an array for this example)
names = Array.new
Also, can do names = Array.new(4, “mac”) for [“mac”, “mac”, “mac”, “mac”]
Ruby array manipulation
- array & other_array - returns new array common to both, with NO DUPLICATES
- array | other_array - returns new array containing elements either in array or other_array, with NO DUPLICATES
- array.clear: removes all elements from array
- array.map! { |item| block }: Invokes block once for each element of self, replacing the element with the value returned by block.
- array.delete(obj) [or] array.delete(obj) { block }: Deletes items from self that are equal to obj. If the item is not found, returns nil. If the optional code block is given, returns the result of block if the item is not found.
- array.delete_at(index)
- array.each { |item| block }: Calls block once for each element in self, passing that element as a parameter.
- array.hash: Computes a hash-code for array. Two arrays with the same content will have the same hash code.
- array.shift: Returns the first element of self and removes it (shifting all other elements down by one). Returns nil if the array is empty.
Ruby - Create a new hash
months = Hash.new
or
H = Hash[“a” => 100, “b” => 200]
hash.delete_if { |key,value| block }
- hash.delete_if { |key,value| block }: Deletes a key-value pair from hash for every pair the block evaluates to true.
- hash.invert: inverts keys and values
- hash.keys: array of the keys
- hash.to_a: Creates a two-dimensional array from hash. Each key/value pair is converted to an array, and all these arrays are stored in a containing array.
- hash.to_s: Converts hash to an array, then converts that array to a string
The features of an object-oriented programming language include:
Data Encapsulation
Data Abstraction
Polymorphism
Inheritance
Ruby is a perfect Object Oriented Programming Language
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 ($)
Initialize method
The initialize method is a standard Ruby class method and works almost same way as constructor works in other object oriented programming languages. The initialize method is useful when you want to initialize some class variables at the time of object creation. This method may take a list of parameters and like any other ruby method it would be preceded by def keyword
class Box def initialize(w,h) @width, @height = w, h end
# instance method
def getArea
@width * @height
end
# accessor methods
def printWidth
@width
end
def printHeight
@height
end
end
3 levels of protection at instance method level
Public Methods − Public methods can be called by anyone. Methods are public by default except for initialize, which is always private.
Private Methods − Private methods cannot be accessed, or even viewed from outside the class. Only the class methods can access private members. Protected Methods − A protected method can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.
Ruby ranges
(1. .5) => 1, 2, 3, 4, 5 (inclusive!)
(1. ..5) => 1, 2, 3, 4 (not inclusive!)
Ranges are objects!
Ruby iterator
Iterators are nothing but methods supported by collections. Objects that store a group of data members are called collections. In Ruby, arrays and hashes can be termed collections.
Iterators return all the elements of a collection, one after the other. We will be discussing two iterators here, each and collect. Let’s look at these in detail.