Ruby Class Review
Tags: beginners, ruby
Published: 2023-07-31 00:38:02

Here I will talk about some of the new things I learned and key takeaways while completing the Ruby class section.

**String Class**

_.gsub("", "")_ replace any occurrence in a string with the first argument with the second argument. 

**Integer Class & Float Class**

When working with large numbers you can use an underscore as a comma for better readability.

ex. 1_000_000

_.odd?_ and _even?_ will come in useful when solving if conditionals.

Using _rand()_ will give you a number from 0 to one less designated number. 

Using _rand(1..6)_ will replicate a die roll

Only one number in your equation needs to be a float number for your output to be a float. 

**Date Class**

To use the built-in date methods in ruby you must first place _require "date"_ above your code

_Date.today_ gives you current date YYYY-MM-DD

To extract the year, month, or day use _.year_, _.month_, _.day_ This will only give you the numerical value back.

Finding the number of days between two dates can be helpful and with ruby it is easy...

```ruby
require "date"
number_of_days = Date.today - Date.parse("July 4, 1776")

pp number_of_days
```

Getting back the weekday, month, AM/PM uses _.strftime_ with special notation

```ruby
# weekday
pp Time.now.strftime("%A")

# month
pp Time.now.strftime("%B")

# month abbreviated
pp Time.now.strftime("%b")

# weekday abbreviated, day of month, time and minutes with AM/PM
pp Time.now.strftime("%a %e, %R %p")

```

**Array Class**

Using _.split("")_ to convert a string into an array is required to solve many ruby coding problems.

An easy way to get a sum of numbers is to put them in an array and use _.sum_

**If Statement Class**

REMEMBER - When using an if statement, it must end with the word end. Make it a habit to start an if statement with both keywords.

```ruby
if
end
```

When two conditions need to be checked use _&&_ for "and"

When more than one condition needs to be checked, but only one needs to be satisfied use _||_ fir "or"

**Loop Class**

Different looping blocks that can be used are...

```ruby
10.times do |variable|
  pp 2 * variable

#the loop block will begin 2 * 1... 2 * 2...... 2 * 10
end
```

```ruby
0.upto(arr.length) do |index|
  if arr[index] == "b"
    arr[index] = "2"
  end
end
```


**Each Class**

My last example could be done with the _.each_ method. _.each_ is meant to loop over an array with ease. 

```ruby
arr.each do |index|
  if arr[index] == "b"
    arr[index] == "2"
  end
end
```

**Hash Class**

A hash is similar to an array except it can hold a key with a corresponding value. Organizes info better than an array. 

Use _.store_ to add a key and value to a hash

```ruby
person1 = Hash.new

person1.store(:first_name, "Sal")
person1.store(:role, "Student")
```

_.fetch_ will catch the value associated with a certain key

```ruby
person1.fetch(:first_name)
person1.fetch(:role, "None provided")
```

Using a second argument for your fetch will stop your program from crashing when a value doesn't exist. 

Displaying all the keys in a hash can be done using the _.keys_ method. 

**Creating your own Class**

You can create your own class that can be recycled as many times as you want.

```ruby
class Person
  attr_accessor :first_name
  attr_accessor :last_name
  attr_accessor :role
end

pp Person.new
```

It is very important to capitalize the word you are using as a class.