I have been learning Ruby for the past week and have absolutely loved it so far. You can simply focus on solving the problem rather than worry about implementing basic functionalities as Ruby provides you most of those. There is a lot of good stuff to talk about but today I'll be going over the Enumerables in Ruby.
A bunch of searching, traversing and sorting methods are provided by the Enumerable mixing in Ruby. We won't be talking about all of them in this blog post but a few which I have found myself frequently using in solving the problems.
First comes the sort method which returns a sorted array based on its own implementation or you can also provide a block with this method to define how comparison between the elements of array is done. Next comes the sort_by method, a block containing the parameters based on which sorting is to be done is supplied with this. Though you should keep in mind that sort_by is fairly expensive, so remember not to stuff your code with sort_by methods.
select is another method which you'll find very useful, it takes a block and returns an array containing all the elements for which block is true. reject is the opposite of select, it takes a block and returns an array containing all the elements for which the block returns false. These two methods are very useful when you want a part of an array based on some conditions. Next up is the map method which returns a new array based on the operations you specify in the block, which are performed on each element. You want to use this method when you have a set of operations to be performed on a bunch of elements.
The last method we are going to discuss in this post is inject. When we specify a block, for each element the block is passed an accumulator value and the element. If we do not specify an initial value for inject, it'll take the first element as the initial value for the accumulator. Finally the accumulator value is returned by the inject method.
You can read more about Enumerables and the methods provided by Ruby at ruby-doc.org
A bunch of searching, traversing and sorting methods are provided by the Enumerable mixing in Ruby. We won't be talking about all of them in this blog post but a few which I have found myself frequently using in solving the problems.
First comes the sort method which returns a sorted array based on its own implementation or you can also provide a block with this method to define how comparison between the elements of array is done. Next comes the sort_by method, a block containing the parameters based on which sorting is to be done is supplied with this. Though you should keep in mind that sort_by is fairly expensive, so remember not to stuff your code with sort_by methods.
select is another method which you'll find very useful, it takes a block and returns an array containing all the elements for which block is true. reject is the opposite of select, it takes a block and returns an array containing all the elements for which the block returns false. These two methods are very useful when you want a part of an array based on some conditions. Next up is the map method which returns a new array based on the operations you specify in the block, which are performed on each element. You want to use this method when you have a set of operations to be performed on a bunch of elements.
The last method we are going to discuss in this post is inject. When we specify a block, for each element the block is passed an accumulator value and the element. If we do not specify an initial value for inject, it'll take the first element as the initial value for the accumulator. Finally the accumulator value is returned by the inject method.
You can read more about Enumerables and the methods provided by Ruby at ruby-doc.org
No comments:
Post a Comment