We have a couple things going on here
gets.sum
is just calling sum
on the string input by gets
and is returning an integer.
The result is simply the sum of the binary value of each byte in str...
/$/
is just a Regexp looking for where the end of the line is
- The
~
operator on a Regexp tells it to operate on the $_
global variable which is the last string input to gets
or readline
. In short this returns the index of the end of the line
Matches rxp against the contents of $_. Equivalent to rxp =~ $_.
- Finally, tying these 2 together, we have a
/
which is just a division operator operating on 2 Integers.
It could have also been written using some spaces to make things more clear
p gets.sum / ~/$/
Note: If this was an average, they added in the value for \n
but then didn't divide it back out:
gets.sum/~/$/
# Hello
# => 102
"Hello".sum / "Hello".length
# => 100
"Hello\n".sum / "Hello".length
# => 102
"Hello\n".sum / "Hello\n".length
# => 85