My name is Antonio Blandin and I'm new to Ruby Programming. With all of my 3 months experience, I would like to share what I used to help me trek through this programming adventure. There are so many sticky notes scattered across my laptop home screen, both physical and digital. Here are some of the main ones I use as reference when deciding how to tackle an issue.
When iterating through an array, .each is always a good go-to method when you don't need the array to be modified. With .map, no matter what action you performed with the elements, the original array will not be modified and it will return the modified array. The method .each will not do that.
When looking for certain elements, there's many ways to go about this. We can use the .include? method to return true or false, letting us know if the given string or character are included or not. To have the actual element return to you, .find is used to grab the first element that compares true to the element that you're looking for. When it's time to grab multiple elements, .select is the way to go. This will iterate through the array and for every element that returns true, it will be added to the output that is returned. Reject does the opposite of select. The output will be the array with the certain elements removed.
each
- ex. [1,2,3].each {|x| print x.to_s+’!’ } prints 1!2!3! returns [1,2,3]
map
- ex. [1,2,3].map {|x| x*3 } returns [3,6,9]
include?
- ex. “Hello”.include? “lo” => true
- ex. “Hello”.include? “ol” =>false
find
- ex. [1,2,3,4].find { |num| num / 2 == 2 } returns 4
select
- ex. [1,2,3,4,5,6,].select { |num| num%2 == 0 } returns [2,4,6]
reject
- ex. [1,2,3,4,5,6].reject { |x| x == 2 || x == 5 } returns [1,3,4,6]
Top comments (0)