Arrays in Javascript vs Ruby
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(:+)
Published on 23 Dec 2022
all tags
100daysofcode activerecord android annoyances api apt arch array artix atom az3w backend bash blog browser bug callback career ci-cd cli cloud code coding config configuration cp crud cryptography css csv database db design devops django docker email erp feelsgood filter fugitive gif gist git gnome gnome pomodoro grep hebrew http ide isbn-fetcher iso javascript job search js kanban kindle koans learning linkedin linux logger manjaro map markdown microservices mobi mtp neovim nodejs nvchad packages panda pastbin patch portfolio post postgres pytest python rails reduce refactoring reflections rest routes rspec ruby salesforce script scripting security sed shell sql string_replacement study tdd terminal testing tmux ttd version_control vim vim sort walkthrough webdev workflow zsh