WT Unit 6 NOTES
WT Unit 6 NOTES
• Syntax • Example
unless conditional x=1
[then] code unless x>=2
[else puts "x is less than 2"
code else
] end puts "x is greater than 2"
end
Ruby case Statement
• Syntax • Example
$age = 5
case expression case $age
[when expression [, expression ...] when 0 .. 2
[then] when 3 .. 6
when 7 .. 12
code ]... [else
puts "baby"
code ] end puts "little child"
puts "child"
when 13 .. 18 puts "youth"
else puts "adult"
end
Ruby- Arrays
• Ruby arrays are ordered, integer-indexed collections of any object.
Each element in an array is associated with and referred to by an
index
• An index of -1 indicates the last element of the array, -2 is the next to
last element in the array, and so on.
• Ruby arrays can hold objects such as String, Integer, Fixnum, Hash,
Symbol, even other Array objects.
Arrays
• cars = Array.new
• cars = Array.new (20)
• CARS= Array.new(3, "Oddi")
Ruby- Array Built-in Methods
Method Example Use
.length array.length The .length method tallies the number of elements in your array and
returns the count
.first array.first The .first method accesses the first element of the array, the element at
index 0
.last array.last The .last method accesses the last element of the array
.take array.take(3) The .take method returns the first n elements of the array
.drop array.drop(3) The .drop method returns the elements after n elements of the array
.pop array.pop The .pop method will permanently remove the last element of an array
.push array.push(98) The .push method will allow you to add an element to the end of an array
Ruby- Hashes
• A Hash is a collection of key-value pairs like this: "employee" =>
"salary". It is similar to an Array, except that indexing is done via
arbitrary keys of any object type, not an integer index.
• If you attempt to access a hash with a key that does not exist, the
method will return nil.
Ruby- Hashes
months = Hash.new
months = Hash.new( "month")
H = Hash["a" => 100,"b" => 200]
Ruby- Hashes Vs Array
Hashes Arrays
whereas hashes support any object as a With arrays, the key is an integer
key
Elements in Hash are not ordered Elements in Array are in ordered
Example: months = Hash.new Example: cars = Array.new
Methods
• Ruby methods prevent us from writing the same code in a program
again and again.
• It is a set of expression that returns a value.
• Defining Method
• Ruby method is defined with the def keyword followed by method name.
• At the end use end keyword.
• Methods name should always start with a lowercase letter
Methods
• Syntax: • Example:
def def welcome
methodName puts “Welcome
code... dear Students”
end end
Methods
• Local variables
• The Local variables either are formal parameters or are variables
created in a method. A variable is created in a method by assigning an
object to it. The scope of a local variable is from the header of the
method to the end of the method.
• Parameters
• The parameter values that appear in a call to a method are called
actual parameters.
• The parameter names used in the method, which correspond to the
actual parameters, are called formal parameters
Classes
• Each Ruby class is an instance of class Class. Classes in Ruby are first-
class objects.
• Ruby class always starts with the keyword class followed by the class
name. Conventionally, for class name we use CamelCase. The class
name should always start with a capital letter. Defining class is
finished with end keyword
Classes
• Syntax: • Example
class Class Vehicle {
ClassName Number no_of_wheels
codes... String Name
end Function speeding {
}
Function driving {
}
}
Classes
• Inheritance
• Subclasses are defined in Ruby with the left angle bracket (<):
• class My_Subclass < Base_class
• Ruby modules provide a naming encapsulation that is often used to
define libraries of methods
Code Blocks
• Ruby code blocks are called closures in other programming languages. It consist of a group of codes
which is always enclosed with braces or written between do..end.
• A block is written in two ways,
1.Multi-line between
do .. end
Example:
do
club.enroll(person
) person.socialize
end
2.Inline between braces { }
Example: { puts
"Hello" }
Iterators- Examples
• The word iterate means doing one thing multiple times.
• “Iterators” is the object-oriented concept in Ruby.
• Iterators are the methods which are supported by collections(Arrays, Hashes
etc.).
• There are many iterators in Ruby as follows:
1. Each Iterator
2. Collect Iterator
3. Times Iterator
4. Upto Iterator
5. Downto Iterator
6. Step Iterator
7. Each_Line Iterator
Iterators- Examples
Each iterator Times iterator Upto iterator downto iterator
Example: Example: Example: Example:
7.times do |i| 7.upto(4) do |i| 7.downto(4) do |i|
(0..9) . each do | i |
puts puts i puts i
puts i
i end end end
end
Ruby- Pattern Matching
• Pattern matching is a feature that is commonly found in functional
programming languages.
• A regular expression is a sequence of characters that define a search
pattern, mainly for use in pattern matching with strings.
• ~ / / This is used for Pattern matching or else can use .match() method
• Example:-
line1 = "Cats are smarter than dogs";
if ( line1 = ~ / Cats( . *) / )
puts "Line 1 contains Cats"
end
Pattern Matching- Regular
Expression
• short expressions for specifying • Modifiers
character ranges • + is for 1 or more characters
• \w is equivalent to [0-9a-zA-Z_] • * is for 0 or more characters
• \d is the same as [0-9] • ? is for 0 or 1 character
• \s matches white space • i is for ignores case when
• \W anything that’s not in [0-9a- matching text.
zAZ_] • m is for matches multiple lines
• \D anything that’s not a number • ^ Beginning of Line
• \S anything that’s not a space • $ End of Line
Pattern Matching- Example