In this code:
arr.select.each_with_index { |_, i| i.even? }
what does pipe underscore mean?
- RubyGuides What Are Ruby Symbols & How Do They Work? A symbol looks like this: Some people confuse symbols with variables, but they have nothing to do with variables… … a symbol is a lot more like a string. So what are the differences between Ruby symbols & strings? Strings are used to work with data. Symbols are identifiers.
Ruby identifiers are consist of alphabets, decimal digits, and the underscore character, and begin with a alphabets(including underscore). There are no restrictions on the lengths of Ruby identifiers.
Most of operators are just method invocation in special form. But some operators are not methods, but built in to the syntax: In addition, assignment operators ( += etc.) are not user-definable. Control structures in Ruby are expressions, and have some value. Ruby has the loop abstraction feature called iterators.
defined? The character set used in the Ruby source files for the current implementation is based on ASCII. The case of characters in source files is significant. All syntactic constructs except identifiers and certain literals may be separated by an arbitrary number of whitespace characters and comments.
_
is a variable name like every other variable name (for example i
).
It is a convention in Ruby to use _
as a variable name or prefix variable names with _
(like _i
) as an indication that you do not plan to use that variable later on.
In your example, each_with_index
yields two values in each step of the iteration: The current element and the current index.
each_with_index { |_, i| i.even? }
The author of the code needed to name both values but decided to indicate with the variable name _
that they do not care about the current value, only about the current index.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With