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
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"
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.
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]
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]
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
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.
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_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