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_numbersthat 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"
Solution Outline:
Define a method
remove_vowelsthat 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
gsubmethod.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.
Solution Outline:
Define a method
read_filethat takes a file name as input.Open the file in read-only mode using the
File.openmethod and assign it to a variable.Read the contents of the file using the
readmethod.Close the file to free up system resources using the
closemethod.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]
Solution Outline:
Define a method
sort_arraythat takes an array of integers as input.Use the
sortmethod 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]
Solution Outline:
Define a method
create_hashthat takes two arrays as input.Use the
zipmethod to combine the keys and values into an array of arrays.Use the
to_hmethod 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
Solution Outline:
Define a method
is_valid_ipthat 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.
Problem 7: String Search
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
Solution Outline:
Define a method
find_stringthat takes a string and a search term as input.Use the
indexmethod 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