> For the complete documentation index, see [llms.txt](https://www.cs4066.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.cs4066.com/unit-6/ruby-problems.md).

# 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`

```ruby
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"

```ruby
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.

```ruby
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]`

```ruby
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]`

```ruby
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`

```ruby
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.

***

## **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`

```ruby
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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.cs4066.com/unit-6/ruby-problems.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
