What does the dot in this line of code mean: 65. + rand (10)

I stumbled upon this line of code and couldn't figure out what the point was. Can someone explain what dot in does 65 . + rand(10)and how it differs from 65 + rand(10)?

For full context, I saw this code inside this char random string generator:

(0...8).map{65.+(rand(25)).chr}.join => "QSILUSPP"
(0...8).map{65.+(rand(25)).chr}.join => "BJIIBQEE"
(0...8).map{65.+(rand(25)).chr}.join => "XORWVKDV"
+4
source share
2 answers

You may notice that there are 2 method calls in the source code - +and chr. I can show it with equivalent code:

65.send(:+, rand(10)).send(:chr) # is the equal to following line:
65.+(rand(10)).chr

This trick creates a chain of methods that allows you to skip parentheses. With parentheses, 65.+(rand(10)).chryou can write as follows:

(65 + rand(10)).chr

chr rand(10), 65. TypeError:

65+(rand(25)).chr
TypeError: String can't be coerced into Fixnum
+2

. Ruby, + , . , , 65 + rand(10), " " 65.+(rand(10)).

- .+, .

+1

All Articles