Ruby Problems

Problem 1: Math Operations

Write a Ruby script that takes two integers as input, adds them together, and returns the result.

Example Input: ruby script.rb 2 3 Expected Output: 5

def add_numbers(a, b)
  # Use the '+' operator to add the two numbers together

end

a = 2
b = 3
puts add_numbers(a, b)

Solution Outline:

  • Define a method add_numbers that takes two integers as input.

  • Use the '+' operator to add the two numbers together.

  • Return the result.

Problem 2: String Manipulation

Write a Ruby script that takes a string as input and returns the string with all vowels removed.

Example Input: "Hello World" Expected Output: "Hll Wrld"

def remove_vowels(input_string)
  # Use regular expressions to replace all vowels (both lowercase and uppercase) with an empty string

end

input_string = "Hello World"
puts remove_vowels(input_string)

Solution Outline:

  • Define a method remove_vowels that takes a string as input.

  • Use regular expressions to match all vowels (both lowercase and uppercase) in the string.

  • Replace each vowel with an empty string using the gsub method.

  • Return the resulting string.


Problem 3: File Reading

Write a Ruby script that reads a file named "input.txt" and prints its contents to the console.

Assumptions:

  • The file exists in the same directory as the script.

  • The file contains only text data.

def read_file(file_name)
  # Open the file in read-only mode ('r') and assign it to a variable

  # Read the contents of the file using the `read` method

  # Close the file to free up system resources

  # Return the contents of the file

end

file_name = "input.txt"
puts read_file(file_name)

Solution Outline:

  • Define a method read_file that takes a file name as input.

  • Open the file in read-only mode using the File.open method and assign it to a variable.

  • Read the contents of the file using the read method.

  • Close the file to free up system resources using the close method.

  • Return the contents of the file.

Problem 4: Array Sorting

Write a Ruby script that takes an array of integers as input, sorts it in ascending order, and prints the sorted array.

Example Input: [3, 1, 2, 6] Expected Output: [1, 2, 3, 6]

def sort_array(input_array)
  # Use the `sort` method to sort the array in ascending order

end

input_array = [3, 1, 2, 6]
puts sort_array(input_array).inspect

Solution Outline:

  • Define a method sort_array that takes an array of integers as input.

  • Use the sort method to sort the array in ascending order.

  • Return the sorted array.


Problem 5: Hash Creation

Write a Ruby script that takes two arrays as input (keys and values) and creates a hash from them. Print the resulting hash.

Example Input: ["name", "age"], ["John", 30] Here is the rest of the combined version:

Problem 5: Hash Creation

Write a Ruby script that takes two arrays as input (keys and values) and creates a hash from them. Print the resulting hash.

Example Input: ["name", "age"], ["John", 30]

def create_hash(keys, values)
  # Use the `zip` method to combine the keys and values into an array of arrays

  # Use the `to_h` method to convert the array of arrays into a hash

end

keys = ["name", "age"]
values = ["John", 30]
puts create_hash(keys, values).inspect

Solution Outline:

  • Define a method create_hash that takes two arrays as input.

  • Use the zip method to combine the keys and values into an array of arrays.

  • Use the to_h method to convert the array of arrays into a hash.

  • Return the resulting hash.

Problem 6: IP Address Validation

Write a Ruby script that takes an IP address as input and returns true if it is valid, and false otherwise.

Example Input: "192.168.1.1" Expected Output: true

def is_valid_ip(ip_address)
  # Use regular expressions to match the IP address format

end

ip_address = "192.168.1.1"
puts is_valid_ip(ip_address)

Solution Outline:

  • Define a method is_valid_ip that takes an IP address as input.

  • Use regular expressions to match the IP address format.

  • Split the IP address into its four parts and convert each part to an integer.

  • Check if each part is within the range of 0-255 using the between? method.


Write a Ruby script that takes a string and a search term as input, and returns the index of the first occurrence of the search term in the string. If the search term is not found, return -1.

Example Input: "Hello World", "World" Expected Output: 6

def find_string(search_term, text)
  # Use the `index` method to find the index of the search term

end

search_term = "World"
text = "Hello World"
puts find_string(search_term, text)

Solution Outline:

  • Define a method find_string that takes a string and a search term as input.

  • Use the index method to find the index of the search term in the string.

  • If the search term is not found, return -1 using the || operator.

Last updated