Range

how can I convert a string '20-30' to a instance range object 20..30

thanks

···

--
Posted via http://www.ruby-forum.com/.

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

HTH,
Sebastian

···

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

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
Rob@AgileConsultingLLC.com

···

On Sep 15, 2009, at 6:11 PM, Sebastian Hungerecker wrote:

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

Or:
    range = string.match(/(\d+)-(\d+)/) and ($1.to_i .. $2.to_i)

···

At 2009-09-15 06:23PM, "Rob Biedenharn" wrote:

On Sep 15, 2009, at 6:11 PM, Sebastian Hungerecker wrote:
> 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

Or:

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

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

Hi --

···

On Wed, 16 Sep 2009, Glenn Jackman wrote:

At 2009-09-15 06:23PM, "Rob Biedenharn" wrote:

On Sep 15, 2009, at 6:11 PM, Sebastian Hungerecker wrote:

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

Or:

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

Or:
   range = string.match(/(\d+)-(\d+)/) and ($1.to_i .. $2.to_i)

Or:

   range = Range.new(*str.scanf("%d-%d"))

David

--
David A. Black, Director
Ruby Power and Light, LLC (http://www.rubypal.com)
Ruby/Rails training, consulting, mentoring, code review
Book: The Well-Grounded Rubyist (http://www.manning.com/black2\)

eval string.sub(/-/, '..')

:slight_smile:

···

--
Posted via http://www.ruby-forum.com/.