1

I am using latest ruby version[ruby 2.4.1p111]

I am getting the result of '123'.to_i +12 to 171

irb(main):021:0> '123'.to_i + 12
=> 135
irb(main):022:0> '123'.to_i +12
=> 171
irb(main):023:0> 123 + 12
=> 135
irb(main):024:0> 123 +12
=> 135

Can you help me understand the second operation here.

2 Answers 2

3

You ended up calling the unary plus operator in the second example, which

Returns the receiver’s value

and then you ended up with (essentially):

'123'.to_i 12

and since, to_i takes an argument, base, you ended up converting '123' to an integer in base 12, which is, apparently, 171.

Sign up to request clarification or add additional context in comments.

Comments

2

to_i can take an argument, the base, and its default is 10.

What you did is:

'123'.to_i(12)

String#to_i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.