# Range

**URL:** <https://www.ruby-forum.com/t/range/94656>\
**Category:** Ruby\
**Created:** [May 10, 2006, 9:14am UTC](https://www.ruby-forum.com/t/range/94656 "2006-05-10T09:14:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [May 10, 2006, 9:14am UTC](https://www.ruby-forum.com/t/range/94656/1 "2006-05-10T09:14:56Z")

</div>

another code example from my book doesnt work with the ruby interpreter

parts = (“Part A” … “Part G”)  
parts.length #â??\> 7

that spits out a error that says it doesnt recognize the length method.

I realize this question is pretty simple but i am a begining programmer.  
I am really enjoying the response time in this forum, i usually get a  
response in about 5 minutes any time of day 😃

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [May 10, 2006, 10:06am UTC](https://www.ruby-forum.com/t/range/94656/2 "2006-05-10T10:06:59Z")

</div>

corey konrad wrote:

> another code example from my book doesnt work with the ruby interpreter
> 
> parts = (“Part A” … “Part G”)  
> parts.length #â??\> 7
> 
> that spits out a error that says it doesnt recognize the length method.
> 
> I realize this question is pretty simple but i am a begining programmer.  
> I am really enjoying the response time in this forum, i usually get a  
> response in about 5 minutes any time of day 😃

Try with [brackets]

parts = [“Part A” , “Part B”, “Part G”]  
p parts.length #â??\> 7

Ytoba

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [May 10, 2006, 10:09am UTC](https://www.ruby-forum.com/t/range/94656/3 "2006-05-10T10:09:46Z")

</div>

On 5/10/06, corey konrad [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> another code example from my book doesnt work with the ruby interpreter
> 
> parts = (“Part A” … “Part G”)  
> parts.length #?\> 7

A range is a specification which can be used for making ordered lists  
(“lists” is not a ruby word). You can’t count the elements of a range  
because there aren’t any, except for the two objects specifying the  
beginning and end of the range.

You can make a “list” from a range. In fact, you can make an array  
from a range and then count the objects in the array.

For example:  
parts = (“Part A” … “Part G”)  
parts\_array = parts.to\_a  
parts.length # → 7

which can be shortened to “parts.to\_a.length”.

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [May 10, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/range/94656/4 "2006-05-10T13:20:38Z")

</div>

2006/5/10, stuart yarus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> You can make a “list” from a range. In fact, you can make an array  
> from a range and then count the objects in the array.
> 
> For example:  
> parts = (“Part A” … “Part G”)  
> parts\_array = parts.to\_a  
> parts.length # → 7
> 
> which can be shortened to “parts.to\_a.length”.

Another nice way to do this is to use inject. IMHO this  
implementation could also go into Enumerable as a default  
implementation:

> > (“Part A” … “Part G”).inject(0) {|s,| s+1}  
> > =\> 7

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [May 10, 2006, 10:25am UTC](https://www.ruby-forum.com/t/range/94656/5 "2006-05-10T10:25:57Z")

</div>

stuart yarus wrote:

> On 5/10/06, corey konrad [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > another code example from my book doesnt work with the ruby interpreter
> > 
> > parts = (“Part A” … “Part G”)  
> > parts.length #ï¿½\> 7
> 
> A range is a specification which can be used for making ordered lists  
> (“lists” is not a ruby word). You can’t count the elements of a range  
> because there aren’t any, except for the two objects specifying the  
> beginning and end of the range.
> 
> You can make a “list” from a range. In fact, you can make an array  
> from a range and then count the objects in the array.
> 
> For example:  
> parts = (“Part A” … “Part G”)  
> parts\_array = parts.to\_a  
> parts.length # → 7
> 
> which can be shortened to “parts.to\_a.length”.

hmm ok i guess the author made a mistake in this book i just wanted to  
check to make sure it wasnt a bug in my ruby or something.

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [May 10, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/range/94656/6 "2006-05-10T19:33:12Z")

</div>

On May 10, 2006, at 12:15 AM, corey konrad wrote:

> another code example from my book doesnt work with the ruby  
> interpreter
> 
> parts = (“Part A” … “Part G”)  
> parts.length #?\> 7
> 
> that spits out a error that says it doesnt recognize the length  
> method.

Range#length was removed between 1.6.8 and 1.8.mumble.

> I realize this question is pretty simple but i am a begining  
> programmer.  
> I am really enjoying the response time in this forum, i usually get a  
> response in about 5 minutes any time of day 😃

Try using ri.

## $ ri Range […] Instance methods:

```
  ==, ===, begin, each, end, eql?, exclude_end?, first, hash,
  include?, inspect, last, member?, step, to_s

```

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
