range

S

Sebastian Hungerecker

Am Mittwoch 16 September 2009 00:06:21 schrieb Re BR:
how can I convert a string '20-30' to a instance range object 20..30

first, last = '20-30'.split("-")
first.to_i .. last.to_i
#=> 20..30

HTH,
Sebastian
 
R

Rob Biedenharn

Am Mittwoch 16 September 2009 00:06:21 schrieb Re BR:

first, last = '20-30'.split("-")
first.to_i .. last.to_i
#=> 20..30

HTH,
Sebastian

Or:

string="20-30"
Range.new(*(string.split('-',2).map{|s|s.to_i}))

in English, split the string into at most two parts at the '-', map
those parts as integers, and use them to build a new Range.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top