SlideShare una empresa de Scribd logo
1 de 3
Descargar para leer sin conexión
RUBY RANGES
http://www.tutorialspoint.com/ruby/ruby_ranges.htm                                               Copyright © tutorialspoint.com



Ranges occur everywhere: January to December, 0 to 9, lines 50 through 67, and so on. Ruby supports ranges and
allows us to use ranges in a variety of ways:

       Ranges as Sequences

       Ranges as Conditions

       Ranges as Intervals

Ranges as Sequences:

The first and perhaps most natural use of ranges is to express a sequence. Sequences have a start point, an end point, and
a way to produce successive values in the sequence.

Ruby creates these sequences using the ''..'' and ''...'' range operators. The two-dot form creates an inclusive range,
while the three-dot form creates a range that excludes the specified high value.

 (1..5)             #==> 1, 2, 3, 4, 5
 (1...5)            #==> 1, 2, 3, 4
 ('a'..'d')         #==> 'a', 'b', 'c', 'd'


The sequence 1..100 is held as a Range object containing references to two Fixnum objects. If you need to, you can
convert a range to a list using the to_a method. Try following example:

 #!/usr/bin/ruby

 $, =", "   # Array value separator
 range1 = (1..10).to_a
 range2 = ('bar'..'bat').to_a

 puts "#{range1}"
 puts "#{range2}"


This will produce following result:

 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
 bar, bas, bat


Ranges implement methods that let you iterate over them and test their contents in a variety of ways:

 #!/usr/bin/ruby

 # Assume a range
 digits = 0..9

 puts digits.include?(5)
 ret = digits.min
 puts "Min value is #{ret}"

 ret = digits.max
 puts "Max value is #{ret}"

 ret = digits.reject {|i| i < 5 }
 puts "Rejected values are #{ret}"

 digits.each do |digit|
    puts "In Loop #{digit}"
end


This will produce following result:

 true
 Min value is 0
 Max value is 9
 Rejected values are 5, 6, 7, 8, 9
 In Loop 0
 In Loop 1
 In Loop 2
 In Loop 3
 In Loop 4
 In Loop 5
 In Loop 6
 In Loop 7
 In Loop 8
 In Loop 9


Ranges as Conditions:

Ranges may also be used as conditional expressions. For example, the following code fragment prints sets of lines from
standard input, where the first line in each set contains the word start and the last line the word end.:

 while gets
    print if /start/../end/
 end


Ranges can be used in case statements:

 #!/usr/bin/ruby

 score = 70

 result = case score
    when 0..40: "Fail"
    when 41..60: "Pass"
    when 61..70: "Pass with Merit"
    when 71..100: "Pass with Distinction"
    else "Invalid Score"
 end

 puts result


This will produce following result:

 Pass with Merit


Ranges as Intervals:

A final use of the versatile range is as an interval test: seeing if some value falls within the interval represented by the
range. This is done using ===, the case equality operator.

 #!/usr/bin/ruby

 if ((1..10) === 5)
   puts "5 lies in (1..10)"
 end

 if (('a'..'j') === 'c')
   puts "c lies in ('a'..'j')"
 end

 if (('a'..'j') === 'z')
puts "z lies in ('a'..'j')"
 end


This will produce following result:

 5 lies in (1..10)
 c lies in ('a'..'j')

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Strings
StringsStrings
Strings
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
 
Unit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structuresUnit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structures
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
strings in c language and its importance
strings in c language and its importancestrings in c language and its importance
strings in c language and its importance
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 

Destacado (8)

19 ruby iterators
19 ruby iterators19 ruby iterators
19 ruby iterators
 
15 ruby arrays
15 ruby arrays15 ruby arrays
15 ruby arrays
 
14 ruby strings
14 ruby strings14 ruby strings
14 ruby strings
 
20 ruby input output
20 ruby input output20 ruby input output
20 ruby input output
 
Ruby File I/O
Ruby File I/ORuby File I/O
Ruby File I/O
 
17 ruby date time
17 ruby date time17 ruby date time
17 ruby date time
 
16 ruby hashes
16 ruby hashes16 ruby hashes
16 ruby hashes
 
8 Key Life and Leadership Lessons
8 Key Life and Leadership Lessons8 Key Life and Leadership Lessons
8 Key Life and Leadership Lessons
 

Similar a 18 ruby ranges

Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
ppparthpatel123
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
Harkamal Singh
 
Hw1 rubycalisthenics
Hw1 rubycalisthenicsHw1 rubycalisthenics
Hw1 rubycalisthenics
shelton88
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
Shankar D
 

Similar a 18 ruby ranges (20)

06 ruby variables
06 ruby variables06 ruby variables
06 ruby variables
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
 
11 ruby methods
11 ruby methods11 ruby methods
11 ruby methods
 
Ruby
RubyRuby
Ruby
 
Codeware
CodewareCodeware
Codeware
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 
Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
 
Hw1 rubycalisthenics
Hw1 rubycalisthenicsHw1 rubycalisthenics
Hw1 rubycalisthenics
 
Ruby training day1
Ruby training day1Ruby training day1
Ruby training day1
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
PERL.ppt
PERL.pptPERL.ppt
PERL.ppt
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Ruby_Basic
Ruby_BasicRuby_Basic
Ruby_Basic
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 

Más de Walker Maidana (14)

13 ruby modules
13 ruby modules13 ruby modules
13 ruby modules
 
12 ruby blocks
12 ruby blocks12 ruby blocks
12 ruby blocks
 
10 ruby loops
10 ruby loops10 ruby loops
10 ruby loops
 
09 ruby if else
09 ruby if else09 ruby if else
09 ruby if else
 
08 ruby comments
08 ruby comments08 ruby comments
08 ruby comments
 
07 ruby operators
07 ruby operators07 ruby operators
07 ruby operators
 
05 ruby classes
05 ruby classes05 ruby classes
05 ruby classes
 
04 ruby syntax
04 ruby syntax04 ruby syntax
04 ruby syntax
 
03 ruby environment
03 ruby environment03 ruby environment
03 ruby environment
 
01 index
01 index01 index
01 index
 
00 ruby tutorial
00 ruby tutorial00 ruby tutorial
00 ruby tutorial
 
02 ruby overview
02 ruby overview02 ruby overview
02 ruby overview
 
21 ruby exceptions
21 ruby exceptions21 ruby exceptions
21 ruby exceptions
 
Tutorial ruby eustaquio taq rangel
Tutorial ruby   eustaquio taq rangelTutorial ruby   eustaquio taq rangel
Tutorial ruby eustaquio taq rangel
 

18 ruby ranges

  • 1. RUBY RANGES http://www.tutorialspoint.com/ruby/ruby_ranges.htm Copyright © tutorialspoint.com Ranges occur everywhere: January to December, 0 to 9, lines 50 through 67, and so on. Ruby supports ranges and allows us to use ranges in a variety of ways: Ranges as Sequences Ranges as Conditions Ranges as Intervals Ranges as Sequences: The first and perhaps most natural use of ranges is to express a sequence. Sequences have a start point, an end point, and a way to produce successive values in the sequence. Ruby creates these sequences using the ''..'' and ''...'' range operators. The two-dot form creates an inclusive range, while the three-dot form creates a range that excludes the specified high value. (1..5) #==> 1, 2, 3, 4, 5 (1...5) #==> 1, 2, 3, 4 ('a'..'d') #==> 'a', 'b', 'c', 'd' The sequence 1..100 is held as a Range object containing references to two Fixnum objects. If you need to, you can convert a range to a list using the to_a method. Try following example: #!/usr/bin/ruby $, =", " # Array value separator range1 = (1..10).to_a range2 = ('bar'..'bat').to_a puts "#{range1}" puts "#{range2}" This will produce following result: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 bar, bas, bat Ranges implement methods that let you iterate over them and test their contents in a variety of ways: #!/usr/bin/ruby # Assume a range digits = 0..9 puts digits.include?(5) ret = digits.min puts "Min value is #{ret}" ret = digits.max puts "Max value is #{ret}" ret = digits.reject {|i| i < 5 } puts "Rejected values are #{ret}" digits.each do |digit| puts "In Loop #{digit}"
  • 2. end This will produce following result: true Min value is 0 Max value is 9 Rejected values are 5, 6, 7, 8, 9 In Loop 0 In Loop 1 In Loop 2 In Loop 3 In Loop 4 In Loop 5 In Loop 6 In Loop 7 In Loop 8 In Loop 9 Ranges as Conditions: Ranges may also be used as conditional expressions. For example, the following code fragment prints sets of lines from standard input, where the first line in each set contains the word start and the last line the word end.: while gets print if /start/../end/ end Ranges can be used in case statements: #!/usr/bin/ruby score = 70 result = case score when 0..40: "Fail" when 41..60: "Pass" when 61..70: "Pass with Merit" when 71..100: "Pass with Distinction" else "Invalid Score" end puts result This will produce following result: Pass with Merit Ranges as Intervals: A final use of the versatile range is as an interval test: seeing if some value falls within the interval represented by the range. This is done using ===, the case equality operator. #!/usr/bin/ruby if ((1..10) === 5) puts "5 lies in (1..10)" end if (('a'..'j') === 'c') puts "c lies in ('a'..'j')" end if (('a'..'j') === 'z')
  • 3. puts "z lies in ('a'..'j')" end This will produce following result: 5 lies in (1..10) c lies in ('a'..'j')