Contents

Miscellaneous

Ruby Types of Iterators

Iterators in Ruby allow you to loop through collections like arrays, hashes, and ranges. Ruby provides several built-in methods to iterate over these collections.

Common Iterators:

1. each: Iterates over each element in a collection.
2. map: Creates a new array by applying a block to each element.
3. select: Returns an array of elements for which the block returns true.
4. reject: Returns an array of elements for which the block returns false.
5. reduce (also known as inject): Combines all elements of an enumerable by applying a binary operation.
6. times: Iterates a given number of times.
7. upto: Iterates from the current number up to the specified number.
8. downto: Iterates from the current number down to the specified number.
9. step: Iterates from the current number to the specified number, incrementing by the step value.

Examples:

1. each: The each iterator returns all elements of an array or hash, one by one.
Syntax:

				
					collection.each do |variable_name|
  # code to iterate
end
				
			

Example:

				
					# Using each iterator with a range
(0..5).each do |i|
  puts i
end

# Using each iterator with an array
letters = ['A', 'B', 'C']
letters.each do |letter|
  puts letter
end

				
			

2. Collect Iterator: The collect iterator returns all elements of a collection, either an array or hash, and can be used to transform elements.
Syntax:

				
					result = collection.collect { |element| block }

				
			

Example:

				
					# Using collect iterator to multiply each element
numbers = [1, 2, 3, 4]
result = numbers.collect { |x| x * 2 }
puts result

				
			

3. Times Iterator: The times iterator repeats a block of code a specified number of times, starting from 0 up to one less than the specified number.
Syntax:

				
					t.times do |i|
  # code to execute
end

				
			

Example:

				
					# Using times iterator
3.times do |i|
  puts i
end

				
			

4. Upto Iterator: The upto iterator starts from a number and continues up to the specified upper limit.
Syntax:

				
					start.upto(limit) do |i|
  # code to execute
end

				
			

Example:

				
					# Using upto iterator
1.upto(3) do |i|
  puts i
end

				
			

5. Downto Iterator: The downto iterator starts from a number and goes down to a specified lower limit.
Syntax:

				
					start.downto(limit) do |i|
  # code to execute
end

				
			

Example:

				
					# Using downto iterator
5.downto(2) do |i|
  puts i
end

				
			

6. Step Iterator: The step iterator is used when you want to skip a specified number of elements in a range during iteration.
Syntax:

				
					range.step(step_value) do |i|
  # code to execute
end

				
			

Example:

				
					# Using step iterator to skip by 2
(0..10).step(2) do |i|
  puts i
end

				
			

7. Each_Line Iterator: The each_line iterator iterates through each line in a string, often used when working with multi-line text.
Syntax:

				
					string.each_line do |line|
  # code to execute
end

				
			

Example:

				
					# Using each_line iterator
"Hello\nWorld\nRuby".each_line do |line|
  puts line
end

				
			

Ruby getters and setters Method

In Ruby, instance variables (denoted by @) are private by default and are not accessible directly outside the class. To expose these variables for encapsulation purposes, Ruby provides getter and setter methods. A getter method retrieves the value of an instance variable, while a setter method assigns a value to it.

Example 1: Simple Getter Method

				
					# Ruby program using getter method
class Website
  # Constructor to initialize the class with a name instance variable
  def initialize(name)
    @name = name
  end

  # Classical getter method
  def name
    @name
  end
end

# Creating an object of the class
site = Website.new("www.example.com")
puts site.name

				
			

Output:

				
					www.example.com

				
			

In this example, the getter method allows access to the @name variable outside the class.

Example 2: Simple Setter Method

				
					# Ruby program using setter method
class Website
  # Constructor to initialize the class with a name instance variable
  def initialize(name)
    @name = name
  end

  # Classical getter method
  def name
    @name
  end

  # Classical setter method
  def name=(name)
    @name = name
  end
end

# Creating an object of the class
site = Website.new("www.example.com")
puts site.name

# Changing the instance variable from outside the class
site.name = "www.updated.com"
puts site.name

				
			

Output:

				
					www.example.com
www.updated.com

				
			

Here, the setter method allows modification of the @name variable from outside the class.

Accessor Methods in Ruby

Writing multiple getter and setter methods manually can make the code verbose, especially as the class grows. Ruby provides a convenient way to create these methods using accessor methods.

  • attr_reader: Automatically generates a getter method.
  • attr_writer: Automatically generates a setter method.
  • attr_accessor: Generates both getter and setter methods.

Example : 

				
					# Ruby Program using accessor for getter and setter
class Website
  # attr_accessor combines both getter and setter
  attr_accessor :url

  # Constructor to initialize the website URL
  def initialize(url)
    @url = url
  end
end

# Creating an object of the class
my_site = Website.new("www.example.com")

# Accessing the website using the getter
puts my_site.url # Output: www.example.com

# Changing the website using the setter
my_site.url = "www.updatedsite.com"

# Accessing the updated website
puts my_site.url # Output: www.updatedsite.com

				
			

Output:

				
					www.example.com
www.updatedsite.com