3

Why this expression evaluating to 13 ?.

I accidentally evaluated this expression (1_2).next instead of (1+2).next which o/p 4 as result.

=> (1_2).next => 13 

Please let me know how this is, as i m new to Ruby

2 Answers 2

9

Ruby allows you to use _ to break up long numbers, for example

123456789 == 123_456_789 

but the latter is a little easier to read, so your code is the same as 12.next

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

4 Comments

what i was concern about that while following the tut for ruby, they explained that all things in ruby are objects so what i presume is even 1 and 2 are objects as well so how ruby concatenates it ??
@swapnesh Ruby's parser is ignoring the _ and seeing only one object. Ruby is not concatenating two objects.
@oldergod was actually following the RoR so thought to cover some very basics of ruby though ..is it the right prth or I just go for the RoR ?
RoR is not a subset or superset of ruby but a collection of ruby libraries (gems). I would recommend understanding the basics of ruby to write simple but non trivial programs.
3

1_2 is the same as 12. 12.next is 13. Underscores in numbers are ignored, you can use them for readability. E.g. 1000_000_000 is one billion.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.