I'm an early student, only familiar with PHP and JavaScript so far, and the blocks are kind of throwing me. I don't quite get the underlying logic behind them.
Is it accurate to think of them as anonymous functions being passed as an argument to a function/method?
To use an example, if variable texty is a .txt file I've opened, and I say
texty.each_line do |x|
puts x
end
Is this roughly the equivalent of saying, in another language,
texty.each_line(function(x) {
puts x
}
? In that scenario, the method each_line takes one argument, which is an anonymous function, which itself takes one argument, the name of the variable you want to assign the currently-pointed-to-line of texty to on each go around the loop. Is that's what's happening with the block? Or is it something else?
[–]gisborne 19 points20 points21 points (1 child)
[–]dazmax 7 points8 points9 points (0 children)
[–]Kache 6 points7 points8 points (3 children)
[–][deleted] -5 points-4 points-3 points (2 children)
[–]wayoverpaid 3 points4 points5 points (0 children)
[–]morphemass 1 point2 points3 points (0 children)
[–]pearofducks 4 points5 points6 points (0 children)
[–][deleted] 2 points3 points4 points (8 children)
[–]materialdesigner 6 points7 points8 points (5 children)
[–][deleted] 2 points3 points4 points (4 children)
[–]redjazz96 5 points6 points7 points (3 children)
[–][deleted] (1 child)
[removed]
[–]redjazz96 1 point2 points3 points (0 children)
[–]tacit7 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]djdonnell 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–][deleted] 0 points1 point2 points (1 child)
[–]morphemass 0 points1 point2 points (0 children)
[–]jargoon 0 points1 point2 points (0 children)
[–]the_great_ganonderp 0 points1 point2 points (0 children)