Leaving the warm blanket of Ruby for JavaScript
Comparing useful methods in both languages
May 2, 2015
After studying Ruby for several weeks, our Dev Bootcamp curriculum has moved to JavaScript.
I'm sad to see Ruby go. It has so much built in "sugar" that makes it easy to write clear and concise code.
But JavaScript dominates the web world, so we need to know it inside and out if we're going to become web devs.
I think it was a great Trojan Horse to get DBC students started with beginner-friendly Ruby and then transition into JavaScript.
I felt pretty naked at first when I was making my way around JavaScript. I felt like I had to build everything from scratch instead of just using the easy Ruby built-ins.
But in researching for this blog, I found I may have been overreacting.
I took a look at some of the most common/powerful array methods in Ruby (select, map, reduce) and found their brothers in JavaScript (filter, map, reduce).
Select and Filter
The idea behind select (Ruby) and filter (JavaScript) is to only return the elements in the array that match your criteria.
Select in Ruby
Filter in JavaScript
Hmmm, these examples look very similar. The biggest difference is that you need that big funky function
declaration in JavaScript. In Ruby you just use the code block inside the curly braces to do that.
Maybe this isn't so bad.
Map
Map is used to return a new array with elements that have been modified by your code block.
Map in Ruby
Map in JavaScript
Once again, these two pieces of code are not that different.
Reduce
Reduce brings the elements of an array down to single value based on your code block.
Reduce in Ruby
Reduce in JavaScript
Yep, you have to write function
and return
explicitly in JavaScript, but otherwise these look very similar.
Conclusion
This is a very superficial exploration for someone transitioning from Ruby to JavaScript, but I think it shows that the languages are similar enough to translate with ease.