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
--
Posted via http://www.ruby-forum.com/.
|
|
0
|
|
|
|
Reply
|
abboura84 (16)
|
2/8/2008 9:36:58 AM |
|
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
|
|
0
|
|
|
|
Reply
|
wieczo.yo (134)
|
2/8/2008 9:55:30 AM
|
|
On Feb 8, 2008 3:36 AM, Abir B. <abboura84@yahoo.fr> 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
|
|
0
|
|
|
|
Reply
|
caduceass (829)
|
2/8/2008 10:19:46 AM
|
|
thanks a lot
I'll try it... :-)
--
Posted via http://www.ruby-forum.com/.
|
|
0
|
|
|
|
Reply
|
abboura84 (16)
|
2/8/2008 10:27:52 AM
|
|
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
|
|
0
|
|
|
|
Reply
|
justincollins (215)
|
2/8/2008 8:29:17 PM
|
|
Thomas Wieczorek 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 Collins 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
--
Posted via http://www.ruby-forum.com/.
|
|
0
|
|
|
|
Reply
|
iw1junk (1195)
|
2/8/2008 9:36:41 PM
|
|
Ian Whitlock wrote:
> Thomas Wieczorek 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 Collins 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
>
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
|
|
0
|
|
|
|
Reply
|
justincollins (215)
|
2/8/2008 10:25:41 PM
|
|
|
6 Replies
38 Views
(page loaded in 0.11 seconds)
Similiar Articles: Convert string : Upper to lower - comp.unix.solarisHi, From shell command, i need to convert un string from lower to upper or upper to lower ... tr "[:lower:]" "[:upper:]" Whereas specifying A-Z as the character range ... Convert string list to number list - comp.soft-sys.math ...Convert List of Numbers to a range string Hi Am using an A2k app in which I want to convert a list of numbers (1,2,3,4,5,8,9,10,12) into something that looks like (1-5, 8 ... help with convert real value to character - comp.lang.fortran ...I've a real value, real :: damp the value can be anything in the range "0.0001 ... convert string to ascii - comp.soft-sys.matlab help with convert real value to character ... std::stringstream and string to unsigned long conversion - comp ...... as the result of converting -1 to an unsigned long is within the range of values ... STRTOUL - convert string to unsigned long integer. (ANSI Standard) Usage ... Converting number to std::string ("itoa()" ) - comp.lang.c++ ...I got it backwards - I was thinking abuot converting a string _into_ a numerical type. ... but may be faster if the argument is known > to be in a valid range." Conversion: double IEEE 754 => decimal ASCII-String - comp.lang ...Or do you need the full "double" precision range of 1.0e-300 to 1.0e+300 (or ... convert string to ascii - comp.soft-sys.matlab Conversion: double IEEE 754 => decimal ... char to string - comp.soft-sys.matlabI have a file name, which is a character array, and i want to place this name into a matrix. To do this, i've tried to convert the char array into a string. Convert month name to month number faster - comp.lang.python ...> import random > l = [random.choice(month_dict.keys()) for _ in range(1000000 ... Convert string list to number list - comp.soft-sys.math ... convert integer to string ... disp results in Warning: Out of range or non-integer values ...disp results in Warning: Out of range or non-integer values truncated during conversion to character. ... You can't concatenate a string with a floating point number ... Convert int to double then back to int? - comp.lang.c++.moderated ...... conversion depends on whether the platform's int can hold values in that > range. ... is how I convert an integer to a ... versa, here are few ways I use to convert string to ... Using the Range Method to Convert String to a RangeIn Microsoft Excel, when you use the GoTo method in a Visual Basic for Applications procedure, you must use a Visual Basic object style reference, such as ... Know how to convert a range to a string? - Wrox Programmer ForumsHi, I've got a range object that I want to convert to a string so I can use in the PivotTableWizard method: Dim rng As Range set rng = Sheets("sal 7/22/2012 1:48:00 AM
|