# Convert String to range

**URL:** <https://www.ruby-forum.com/t/convert-string-to-range/128512>\
**Category:** Ruby\
**Created:** [February 8, 2008, 10:37am UTC](https://www.ruby-forum.com/t/convert-string-to-range/128512 "2008-02-08T10:37:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [February 8, 2008, 10:37am UTC](https://www.ruby-forum.com/t/convert-string-to-range/128512/1 "2008-02-08T10:37:05Z")

</div>

Hi,  
is there a way to convert a string to a range??  
“(1…2)” ==\> (1…2) ??  
Another question, what’s the translation of infinite in ruby??  
thanks

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [February 8, 2008, 10:56am UTC](https://www.ruby-forum.com/t/convert-string-to-range/128512/2 "2008-02-08T10:56:29Z")

</div>

I tried to monkey patch the Range class, but it doesn’t seem to work,  
so I patched the String class:

class String  
def to\_range  
case self.count(’.’)  
when 2  
elements = self.split(’…’)  
return Range.new(elements[0].to\_i, elements[1].to\_i)  
when 3  
elements = self.split(’…’)  
return Range.new(elements[0].to\_i, elements[1].to\_i-1)  
else  
raise ArgumentError.new(“Couldn’t convert to Range:  
#{str}”)  
end  
end  
end

p “1…2”.to\_range  
p “1…2”.to\_range  
p “1.2”.to\_range

output:  
1…2  
1…1  
rng.test:11:in `to_range': undefined local variable or method`str’  
for “1.2”:String (NameError)  
from rng.test:18

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [February 8, 2008, 11:20am UTC](https://www.ruby-forum.com/t/convert-string-to-range/128512/3 "2008-02-08T11:20:44Z")

</div>

On Feb 8, 2008 3:36 AM, Abir B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Another question, what’s the translation of infinite in ruby??  
> thanks

irb(main):001:0\> 0 / 0.0  
=\> NaN  
irb(main):002:0\> 42 / 0.0  
=\> Infinity  
irb(main):003:0\> -42 / 0.0  
=\> -Infinity

You can also use the #nan? and #infinity? methods. AFAIK, this works  
only with Floats and BigDecimals.

hth,  
Todd

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [February 8, 2008, 11:27am UTC](https://www.ruby-forum.com/t/convert-string-to-range/128512/4 "2008-02-08T11:27:59Z")

</div>

thanks a lot  
I’ll try it… 🙂

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [February 8, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/convert-string-to-range/128512/5 "2008-02-08T22:36:47Z")

</div>

Thomas W. wrote:

> I tried to monkey patch the Range class, but it doesn’t seem to work,  
> so I patched the String class:

I am not sure what the rules for the request should be, but

p “(1..2)”.to\_range # 0..2 original request  
p “1.2 - 7.2 + 3”.to\_range # 1..0  
p ‘“a”..“c”’.to\_range # 0..0

all produce things I wouldn’t want.

I see Justin C. got in the eval suggestion before I  
finished looking at it. I note that

eval(“1…7”)

causes a syntax mistake. Is there a way to handle syntax  
problems parallel to execution errors?

Ian

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [February 8, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/convert-string-to-range/128512/6 "2008-02-08T21:29:47Z")

</div>

Abir B. wrote:

> Hi,  
> is there a way to convert a string to a range??  
> “(1..2)” ==\> (1..2) ??  
> Another question, what’s the translation of infinite in ruby??  
> thanks

Not recommended, but eval is always an option:

irb(main):001:0\> r = eval(“(1..2)”)  
=\> 1..2  
irb(main):002:0\> r.class  
=\> Range  
irb(main):003:0\> r.to\_a  
=\> [1, 2]

-Justin

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [February 8, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/convert-string-to-range/128512/7 "2008-02-08T23:26:22Z")

</div>

Ian W. wrote:

> p “1.2 - 7.2 + 3”.to\_range # 1..0  
> problems parallel to execution errors?
> 
> Ian

This is a pretty strange way to go about it, but it will work even in  
the presence of errors:

require ‘thwait’

def make\_range(str)  
r = nil  
thr = Thread.new do  
$SAFE = 4  
begin  
r = eval(str)  
rescue  
end  
end

```
    ThreadsWait.new(thr).all_waits

    r.is_a?(Range) ? r : nil

```

end

p make\_range(“1…7”) # nil  
p make\_range(“(1..2)”) # 1..2  
p make\_range(“1.2 - 7.2 + 3”) # nil  
p make\_range(‘“a”..“c”’) # “a”..“c”  
p make\_range(“exit!”) # nil  
p make\_range(“load ‘evilfile.rb’”) # nil

For some reason, trying to use Thread.join will prevent the exception  
handling from working.

-Justin
