Arrays in Javascript vs Ruby
Published on 23 Dec 2022
Setup in Javascript
const array = [1, 2, 10, 16];
const double = [];
const newArray = array.forEach((num) => {
double.push(num * 2);
});
Setup in Ruby
ary = [1, 2, 10, 16]
new_array = []
ary.each { |num| double << num * 2 }
map in Javascript
const array = [1, 2, 10, 16];
const mapArray = array.map(num => num * 2);
map in Ruby
array = [1, 2, 10, 16]
map_array = array.map { |num| num * 2 }
filter in Javascript
const array = [1, 2, 10, 16];
const filterArray = array.filter(num => num > 5);
filter/select in Ruby
array = [1, 2, 10, 16]
select_array = array.select { |num| num > 5 }
reduce in Javascript
const array = [1, 2, 10, 16];
const reduceArray = array.reduce((accumulator, num) => {
return accumulator + num
}, 0);
// 1 + 2 + 10 + 16
// 29
reduce in Ruby
array = [1, 2, 10, 16]
reduce_array = array.reduce(0) { |accumulator, num| accumulator + num }
reduce_array = array.reduce(:+)
all tags
activerecord android annoyances api apt arch array artix atom az3w backend bash blog browser bug callback career cli cloud code coding config configuration cp crud css database db design devops django email erp filter fugitive gif gist git gnome grep hebrew http ide isbn-fetcher iso javascript job search js kanban kanban\ kindle koans linux logger manjaro map markdown microservices mobi mtp neovim nodejs packages pastbin patch post python rails reduce refactoring rest routes rspec ruby scripting security sed shell sql string_replacement study tdd terminal testing version_control vim walkthrough workflow