Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

61
Views
Not get point Ruby operator with dot

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!

10 months ago · Santiago Trujillo
1 answers
Answer question

0

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
10 months ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.