Bhushan Lodha
Bhushan Lodha

Reputation: 6862

Range class in ruby source code?

Where can I find methods of Range class in Ruby's source code. I am particularly looking for Range#last method. Ruby -v MRI 1.9.2

Upvotes: 0

Views: 298

Answers (2)

Pritesh Jain
Pritesh Jain

Reputation: 9146

You can find the Ruby source here

https://github.com/ruby/ruby/

It has a range.c in its root directory.

and for the Range#last implementation in source, I think it is here:

https://github.com/ruby/ruby/blob/trunk/range.c#L602

http://www.ruby-doc.org/core-1.9.3/Range.html#method-i-last

Upvotes: 6

Jörg W Mittag
Jörg W Mittag

Reputation: 369458

Personally, I very much prefer looking at Rubinius's source code, because I find it much more readable than YARV's. The method you are looking for is in kernel/common/range.rb, and it is simply a getter for the @end instance variable:

attr_reader :end
alias_method :last, :end

The @end instance variable is set in the initialize method. In other words: the last method simply always returns the value that you passed to Range.new.

Note that, in this case, Rubinius's implementation conforms to the Ruby 1.8 language specification and hasn't been updated to the Ruby 1.9 specification yet, which has an optional count parameter.

Upvotes: 2

Related Questions