javascript - Call function for all array elements without passing item as parameter -


in javascript can this:

var show = function(w) {   alert(w); };  var words =  ['a', 'b', 'c']; words.foreach(show); 

note don't need pass array item parameter.

is there way same in ruby?

like:

def show(w)   puts w end  words = ['a', 'b', 'c'] words.each(show) 

ps: know can doing: words.each { |w| show(w) }. question if can in javascript, without passing item parameter.

sure, passing method, in ecmascript:

words.each(&method(:show)) 

note way make ruby example work. however, ruby method not closest analogue ecmascript function in ruby. proc closer match:

show = -> w { puts w }  words.each(&show) 

this reads close ecmascript 6 version:

const show = w => alert(w);  words.foreach(show); 

Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -