SlideShare ist ein Scribd-Unternehmen logo
1 von 21
RUBY BASICS - II
Abstract of
Session
 Loops
 Iterators & Blocks
 Array
 Hashes
 Iteration over Array and
Hashes
 Symbols

l19/12/11
Loops ?

Loops in Ruby are used to execute the same block of code a
specified number of times until some condition is met.

l19/12/11
While Loop
While loops will execute all of the statements contained within them as long as the
conditional statement remains true.
There are 3 ways to use while loop
Syntax :
1)

2)
3)

while condition do
code
end
code while condition
begin
code
end while condition

l19/12/11
Example

What is difference ?
In 2nd or 3rd case, code is executed once before conditional is evaluated.
example :
num = 0
max = 5
while num < max
puts("Inside the loop #{num}")
num += 1
end

l19/12/11
until

Executes code when condition is false.
There are 3 ways to use until loop
Syntax :
1) until condition do
code
end
2)
3)

code until condition
begin
code
end until condition

l19/12/11
For
Loop

lexample :
l

•for num in 0..5
puts("Inside the loop #{num}")
•end

lO/P :
•Inside the loop 0
•Inside the loop 1
•Inside the loop 2
•Inside the loop 3
•Inside the loop 4
•Inside the loop 5

l19/12/11
times

lexample :
l

• 5.times do |index|
puts("Inside the loop #{index}")
•end

lO/P :
•Inside the loop 0
•Inside the loop 1
•Inside the loop 2
•Inside the loop 3
•Inside the loop 4

l19/12/11
What are
iterators ?
l Another word for loop. In ruby, Iterators are nothing but methods supported by
collections.
l Objects that store a group of data members are called collections. In Ruby, arrays and
hashes can be termed collections.
l Iterators return all the elements of a collection, one after another. We will be discussing
two iterators here.
• each
• collect

l19/12/11
each

lSyntax :
•collection.each do |variable|
l
code
•end
lExample :

l

•num_array = [1,2,3,4,5]
•num_array.each do |num|
puts num
•end

lYou always associate the each iterator with a block. It returns each value of the
array, one by one, to the block. The value is stored in the variable num and then
displayed on the screen.

l19/12/11
collect

lThe collect iterator returns all the elements of a collection.
lSyntax :

•collection = collection.collect
lThe collect method need not always be associated with a block. The collect method
returns the entire collection, regardless of whether it is an array or a hash.

lexample :
•num_array = [1,2,3,4,5]
•new_array = num_array.collect{|num| 10*num}
•puts new_array

l19/12/11
What are
blocks?
lA block consists of chunks of code.
lYou assign a name to a block.
lThe code in the block is always enclosed within braces {}.
lA block is always invoked from a function with the same name as that of the block.
This means that if you have a block with the name test, then you use the function test
to invoke this block.
lYou invoke a block by using the yield statement.

l19/12/11
Blocks

lSyntax :
•block_name{
l
statement1
l
statement2
l
..........
•}
lExample :
•def test
l
puts "You are in the method"
l
yield
l
puts "You are again back to the method"
l
yield
•end
•test {puts "You are in the block"}

l19/12/11
Hashes

lA 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.
lAs with arrays, there is a variety of ways to create hashes. You can create an empty hash
with the new class method:
lmonths = Hash.new
lExample:
•num = Hash["a" => 100, "b" => 200]
•keys = num.keys
•puts keys

l19/12/11
Questions ?
Thank You

Weitere ähnliche Inhalte

Was ist angesagt?

เกมส์จับคู่
เกมส์จับคู่เกมส์จับคู่
เกมส์จับคู่
Aeew Autaporn
 
Presentation mcrl2
Presentation mcrl2Presentation mcrl2
Presentation mcrl2
matifch
 

Was ist angesagt? (15)

Loops in R
Loops in RLoops in R
Loops in R
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
694 lecture1aes
694 lecture1aes694 lecture1aes
694 lecture1aes
 
Elementary vim tricks
Elementary vim tricksElementary vim tricks
Elementary vim tricks
 
เกมส์จับคู่
เกมส์จับคู่เกมส์จับคู่
เกมส์จับคู่
 
Loop c++
Loop c++Loop c++
Loop c++
 
Aes
AesAes
Aes
 
MCRL2
MCRL2MCRL2
MCRL2
 
MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2
 
Presentation mcrl2
Presentation mcrl2Presentation mcrl2
Presentation mcrl2
 
Thread safety
Thread safetyThread safety
Thread safety
 
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
DConf 2016: Bitpacking Like a Madman by Amaury SechetDConf 2016: Bitpacking Like a Madman by Amaury Sechet
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
 
Certifying (RISC) Machine Code Safe from Aliasing (OpenCert 2013)
Certifying (RISC) Machine Code Safe from Aliasing  (OpenCert 2013)Certifying (RISC) Machine Code Safe from Aliasing  (OpenCert 2013)
Certifying (RISC) Machine Code Safe from Aliasing (OpenCert 2013)
 
For
ForFor
For
 
Python your new best friend
Python your new best friendPython your new best friend
Python your new best friend
 

Ähnlich wie Ruby basics || updated

Blocks and loops.pptx
Blocks and loops.pptxBlocks and loops.pptx
Blocks and loops.pptx
sandeep kumar
 
Programming using Open Mp
Programming using Open MpProgramming using Open Mp
Programming using Open Mp
Anshul Sharma
 

Ähnlich wie Ruby basics || updated (20)

Ruby basics ||
Ruby basics ||Ruby basics ||
Ruby basics ||
 
12 ruby blocks
12 ruby blocks12 ruby blocks
12 ruby blocks
 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
 
Basic vbscript for qtp
Basic vbscript for qtpBasic vbscript for qtp
Basic vbscript for qtp
 
19 ruby iterators
19 ruby iterators19 ruby iterators
19 ruby iterators
 
5 Statements and Control Structures
5 Statements and Control Structures5 Statements and Control Structures
5 Statements and Control Structures
 
Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
 
Implementation of Various Cryptosystem Using Chaos
Implementation of Various Cryptosystem Using ChaosImplementation of Various Cryptosystem Using Chaos
Implementation of Various Cryptosystem Using Chaos
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16
 
C Programming : Arrays and Functions
C Programming : Arrays and FunctionsC Programming : Arrays and Functions
C Programming : Arrays and Functions
 
Blocks and loops.pptx
Blocks and loops.pptxBlocks and loops.pptx
Blocks and loops.pptx
 
VB Script Overview
VB Script OverviewVB Script Overview
VB Script Overview
 
Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
 
Programming using Open Mp
Programming using Open MpProgramming using Open Mp
Programming using Open Mp
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSVISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
 
Java Pitfalls and Good-to-Knows
Java Pitfalls and Good-to-KnowsJava Pitfalls and Good-to-Knows
Java Pitfalls and Good-to-Knows
 
genalg
genalggenalg
genalg
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Imp_Points_Scala
Imp_Points_ScalaImp_Points_Scala
Imp_Points_Scala
 
Vbscript
VbscriptVbscript
Vbscript
 

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

Ruby basics || updated

  • 2. Abstract of Session  Loops  Iterators & Blocks  Array  Hashes  Iteration over Array and Hashes  Symbols l19/12/11
  • 3. Loops ? Loops in Ruby are used to execute the same block of code a specified number of times until some condition is met. l19/12/11
  • 4. While Loop While loops will execute all of the statements contained within them as long as the conditional statement remains true. There are 3 ways to use while loop Syntax : 1) 2) 3) while condition do code end code while condition begin code end while condition l19/12/11
  • 5. Example What is difference ? In 2nd or 3rd case, code is executed once before conditional is evaluated. example : num = 0 max = 5 while num < max puts("Inside the loop #{num}") num += 1 end l19/12/11
  • 6. until Executes code when condition is false. There are 3 ways to use until loop Syntax : 1) until condition do code end 2) 3) code until condition begin code end until condition l19/12/11
  • 7. For Loop lexample : l •for num in 0..5 puts("Inside the loop #{num}") •end lO/P : •Inside the loop 0 •Inside the loop 1 •Inside the loop 2 •Inside the loop 3 •Inside the loop 4 •Inside the loop 5 l19/12/11
  • 8. times lexample : l • 5.times do |index| puts("Inside the loop #{index}") •end lO/P : •Inside the loop 0 •Inside the loop 1 •Inside the loop 2 •Inside the loop 3 •Inside the loop 4 l19/12/11
  • 9. What are iterators ? l Another word for loop. In ruby, Iterators are nothing but methods supported by collections. l Objects that store a group of data members are called collections. In Ruby, arrays and hashes can be termed collections. l Iterators return all the elements of a collection, one after another. We will be discussing two iterators here. • each • collect l19/12/11
  • 10. each lSyntax : •collection.each do |variable| l code •end lExample : l •num_array = [1,2,3,4,5] •num_array.each do |num| puts num •end lYou always associate the each iterator with a block. It returns each value of the array, one by one, to the block. The value is stored in the variable num and then displayed on the screen. l19/12/11
  • 11. collect lThe collect iterator returns all the elements of a collection. lSyntax : •collection = collection.collect lThe collect method need not always be associated with a block. The collect method returns the entire collection, regardless of whether it is an array or a hash. lexample : •num_array = [1,2,3,4,5] •new_array = num_array.collect{|num| 10*num} •puts new_array l19/12/11
  • 12. What are blocks? lA block consists of chunks of code. lYou assign a name to a block. lThe code in the block is always enclosed within braces {}. lA block is always invoked from a function with the same name as that of the block. This means that if you have a block with the name test, then you use the function test to invoke this block. lYou invoke a block by using the yield statement. l19/12/11
  • 13. Blocks lSyntax : •block_name{ l statement1 l statement2 l .......... •} lExample : •def test l puts "You are in the method" l yield l puts "You are again back to the method" l yield •end •test {puts "You are in the block"} l19/12/11
  • 14.
  • 15.
  • 16.
  • 17.
  • 18. Hashes lA 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. lAs with arrays, there is a variety of ways to create hashes. You can create an empty hash with the new class method: lmonths = Hash.new lExample: •num = Hash["a" => 100, "b" => 200] •keys = num.keys •puts keys l19/12/11
  • 19.