Range

how can I convert a string ‘20-30’ to a instance range object 20…30

thanks

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

On Sep 15, 2009, at 6:11 PM, Sebastian H. 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}))

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 B. http://agileconsultingllc.com
[email protected]

At 2009-09-15 06:23PM, “Rob B.” wrote:

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)

Hi –

On Wed, 16 Sep 2009, Glenn J. wrote:

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

eval string.sub(/-/, ‘…’)

:slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs