Tech

Enumerables–A Cleaner Way to Iterate!!! [Ruby Programming Language Notes]

ENUMERABLES

Enumerables–a cleaner way to iterate.

Enumerable Methods–a way to quickly iterate thru an array or string array.

  • array
    • .each The ‘.each’ method takes in a {block-of-code} instead of (parameters). ‘.each’ passes on just the element.
      • {block-of-code}–uses curly brackets{} to represent it.
    • .each_with_index The ‘.each_with_index’ passes on the element, with the index, to the block of code.
  • string
    • .each_char
    • .each_char.with_index
  • Range enumerable–allows us to control where we start iterating & where we stop.
    • (start..end).each iterate from start to end (inclusive!)
    • (start…end).each iterate from start to end (excluding end!)
    • A range can also be used on letters & other patterns, not just numbers!)
  • Array Syntax for it: months = [ “Jan”, “Feb”, “Mar”, “Apr” ]
  • For single-lines of code:
    • months.each { |month| puts month }
    • Here, the word between the “pipes” is called a ‘block parameter’. In this example, ‘|month|’ is the block parameter. This block parameter behaves kind of like a variable, and changes from one element to the next element on every iteration.
  • Another syntax way is to do a multi-line block:
  • For multiple-lines of code:
    • months.each do |month|
      • puts month
      • puts “—“
    • end
  • String syntax for it:
    • sentence = “hello world”
    • Multi-line block for iterating each character of the sentence string.
    • sentence.each_char do |char|
      • puts char
    • end
  • Array syntax for .each_with_index:
    • months.each_with_index do |ele, idx|
      • puts ele
      • puts idx
      • puts “—“
    • end
  • String syntax for .each_char.with_index:
    • sentence.each_char.with_index do |char, idx|
      • puts char
      • puts idx
      • puts “—“
    • end

Loops with ‘Break’ & ‘Next’ Keywords

  • Loops with ‘Break’ & ‘Next’ Keywords
    • i = 1
    • while i < = 10
    • puts i
    • if i == 5
      • break
    • end
    • puts i
    • i += 1
    • end

Break‘ will immediately exit the loop. The output of the above would be:

  • 1
  • 1
  • 2
  • 2
  • 3
  • 3
  • 4
  • 4
  • 5
  • (Immediately exits the loop!)

Next‘ skips to the next iteration. (Remember that any code in the loop after the “next” keyword won’t run because it will be skipped, including any incrementation that’s place after instead of before!)

  • i = 1
  • while i < = 10
    • puts i
  • if i == 5
    • i += 5
    • next
  • end
  • puts i
    • i += 1
  • end

This would output the following:

  • 1
  • 1
  • 2
  • 2
  • 3
  • 3
  • 4
  • 4
  • 5 (Note only one ‘5’!)
  • 6
  • 6
  • 7
  • 7
  • 8
  • 8
  • 9
  • 9
  • 10
  • 10

Other Enumerable Methods:

  • .times #Repeat a block using times.
    • ex: 3.times do
      • puts “hit”
    • end

This would output:

  • hi
  • hi
  • hi

More ‘RANGE’ use cases…

  • (4..8) –# all number 4 through 8
  • (4..8). to_a –# to array
  • returns this –> [4,5,6,7,8]

We can also use this strategy on letters:

  • (‘b’..’f’).to_a #create an array of letters ‘b’ through ‘f’
  • returns –> [“b”,”c”,”d”,”e”,”f”]
  • This can also be used to create an array of the entire alphabet from (‘a’..’z’).to_a