I'm familiar with Ruby operator and its work flow and use cases but at a place I'm confused with . in expression.
3.**2+1
is giving result 27
instead 3.**2
gives 9
and
3**2+1
gives 10
it is okay but I'm confuse on 3.**2+1
anything that I'm missing with dot . operator here.
Thanks in advance!
Operators in ruby are backed by methods. So 3 ** 2
is functionally equivalent to "call method **
on 3
and pass 2
as an argument".
You can invoke these methods directly too (1.+(2)
instead of 1+2
), which is what that dot does in this case. And because operators have different precedence than method calls, you're seeing these different results.
3.** 2 + 1 == 3.**(2+1)
# but
3 ** 2 + 1 == (3 ** 2) + 1