Methods and Miscellaneous Pt. 1

James Hoggard
3 min readJun 25, 2021

Ruby and JavaScript

As I think about what is now half of a year that has passed I continually reflect on the coding languages that I have primarily studied while in a Software Engineering program and how to further that learning when it’s over. As with any leap forward a person needs to use the foundation currently supporting them, whether it be soft, hard, or somewhere in between. With that said I think that it is important to spend a little time going over what you know so that you can avoid needless mistakes with future projects. As a student of Flatiron, those languages have primarily been Ruby and JavaScript, two languages that I truly enjoy learning about and building with.

Part of what has been fun has been watching the synergy after chaining multiple methods together or just seeing them work alone. Another thing that I’ve liked has been seeing effective gems in action. Here are some methods, loops, and miscellaneous tips on both languages that you may find helpful.

.filter

.filterwill create a new array by basically gathering the elements in the initial array that meet a certain condition that you set. I know that when I was first learning .filter the naming convention was throwing me off. I kept thinking about filtering out being the main action. However if you think of .filter.filter like a strainer that’s gathering ingredients(elements) that meet the specific condition that you set, then it’s easier to see how the method operates. It’s good to keep in mind that .filter will return a different size array while methods like .map and .sort will return same size arrays. Also note that .map is a very handy function when you are ready to make React components out of the data.

map

Remember that by using methods defined by ActiveRecord::Base we can create an object using both the new method and the create method.

Still, knowing the difference can allow you to make changes prior to the object being saved to the database. If you do not want to make any alterations to the object then the create method is perfect. In the case that you do want to update the object’s properties, the new method will allow you to instantiate the object and make alterations before the need for the .save method to finalize those commitments to the database.

limit

.limit is helpful as it will specify a limit for the number of database records to be retrieved.

Person.limit(10)

order

Use this instance method with an order attribute to arrange the query.

Person.order(email: :desc)

--

--