DEV Community

salcasta
salcasta

Posted on

Ruby Class Review

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

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

pp number_of_days
Enter fullscreen mode Exit fullscreen mode

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

# 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")

Enter fullscreen mode Exit fullscreen mode

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.

if
end
Enter fullscreen mode Exit fullscreen mode

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

10.times do |variable|
  pp 2 * variable

#the loop block will begin 2 * 1... 2 * 2...... 2 * 10
end
Enter fullscreen mode Exit fullscreen mode
0.upto(arr.length) do |index|
  if arr[index] == "b"
    arr[index] = "2"
  end
end
Enter fullscreen mode Exit fullscreen mode

Each Class

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

arr.each do |index|
  if arr[index] == "b"
    arr[index] == "2"
  end
end
Enter fullscreen mode Exit fullscreen mode

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

person1 = Hash.new

person1.store(:first_name, "Sal")
person1.store(:role, "Student")
Enter fullscreen mode Exit fullscreen mode

.fetch will catch the value associated with a certain key

person1.fetch(:first_name)
person1.fetch(:role, "None provided")
Enter fullscreen mode Exit fullscreen mode

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.

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

pp Person.new
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)