Ruby Array basic and advance methods
Hi, This is Shubham Mishra from India, this is the part of Ruby on Rails exploration journey. In this post we will discuss about the ruby array's some basic and advance methods and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started...
Ruby Array basic methods
- push - adds an element to the end of the array
- pop - removes the last element from the array and returns it
- shift - removes the first element from the array and returns it
- unshift - adds an element to the beginning of the array less
- length - returns the number of elements in the array
- join - converts the array into a string, with an optional separator
- include? - returns true if the array contains a specified element
- reverse - returns a new array with the elements in reverse order
- sort - returns a new array with the elements sorted in ascending order
- uniq - returns a new array with duplicate elements removed
Ruby Array Advance methods
- map - returns a new array with the result of applying a block to each element in the array
- select - returns a new array with only the elements that satisfy a condition specified in a block
- reject - returns a new array with only the elements that do not satisfy a condition specified in a block
- reduce - returns a single value that is the result of applying a block to each element in the array and accumulating the results
- each_with_index - iterates over each element in the array and provides access to the index of each element
- zip - returns a new array by combining elements from the original array with elements from one or more additional arrays
- sort_by - returns a new array sorted based on a property or result of a block applied to each element
- partition - returns a pair of arrays, one with elements that satisfy a condition specified in a block, and another with the remaining elements
- First, we use the map method to transform the temperatures array into a nested array temperatures_with_days, where the first element is the day index (starting at 0) and the second element is the temperature. We use the with_index method to access the index of each element while mapping over the array.
- Next, we use the group_by method to group the nested arrays by the day index, resulting in an array temperatures_by_day of nested arrays where the first element is the day index and the second element is an array of temperatures for that day.
- Then, we use the map method to iterate over each nested array in temperatures_by_day and calculate the average temperature for that day. We use the reduce method to sum up the temperatures in the nested array and divide by the length of the nested array to get the average temperature. Finally, we use the round method to round the average temperature to the nearest integer.
- We assign the resulting array of average temperatures to the average_temperatures variable and output it using puts.
Get to know answers for common search on Google : A blog for posts which can help you for daily life problems, such as where to get free images, Topic suggestion for the blog.
Computer Science algorithms and other knowledge share : A blog for posts such as best search algorithm, Top interview questions for diffrent technologies, knowledge share for some frameworks or programming languages for the interview or in general terms.
My ideas to solve real world problems : A blog where me shared and presented my ideas to solve a real world problems, this will be interesting for me.
Ruby on Rails Web development Blog : As the name suggest, it is the blog for sharing few knowledge about RoR web development framework.
Liked my blogs, wanna to connect:
Thanks for reading this post, Have a good day :)
Comments
Post a Comment