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
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")
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
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
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.
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
person1 = Hash.new
person1.store(:first_name, "Sal")
person1.store(:role, "Student")
.fetch will catch the value associated with a certain key
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.
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.
Top comments (0)