English 中文(简体)
鲁比:将“强硬”改为“煽动”,或在必要时将之作为“强硬”?
原标题:Ruby: Best way to convert a String to Integer or leave it as String if necessary?

Developing a little survey webapp, ran into problem that deals with ranges for rating type questions. So a rating s range could be:

1..10
-5..0
-5..5
 a .. z 
 E .. M 

页: 1

范围被储存在数据库中(范围开始和结束)。 因此,范围总是作为强有力的投入而开始。

What is the best way to take these string values and build a Ruby Range accordingly. I can t just go value.to_i as this won t work for string iteration. Having a bunch of if s seems ugly. Any better way?

Not as important, but worth asking: Also what if I wanted to make it all work with reversed range? Say 5-to-0 or G-to-A. I know that Ruby doesn t support reverse range (since it uses succ() to iterate). What would be the best way here?

提前感谢!

最新情况:

根据Wouter de Bie的建议,我就此作了决定:

def to_int_or_string(str)
  return str.match(/^-?d+$/) ? str.to_i : str.strip
end

def ratings_array(from, to)
  from = to_int_or_string(from)
  to = to_int_or_string(to)
  from > to ? Range.new(to, from).to_a.reverse : Range.new(from, to).to_a
end

任何想法?

最佳回答

使用范围:新

Range.new("a","z")
=> "a".."z"

Range.new(-5,5)
=> -5..5

如果你有quot的话,你可以使用电子阀来达到正确的幅度:

from = " a "
to = " z "
eval("Range.new(#{from},#{to})")

否则,你就可以使用价值。 a. 如果是var中的一种或数种,请说明:

a = "x"
a = (a.to_i == 0 && a != "0") ? a : a.to_i
=> "x"

a = "5"
a = (a.to_i == 0 && a != "0") ? a : a.to_i
=> 5

哪一种方法当然可以归纳为:

def to_int_or_string(value)
  return (value.to_i == 0 && value != "0") ? value : value.to_i
end

def to_range(from, to)
  return Range.new(to_int_or_string(from), to_int_or_string(to))
end

为了扭转你的范围,你必须首先将其转化为一个阵列:

Range.new("a","g").to_a.reverse
=> ["g", "f", "e", "d", "c", "b", "a"]
问题回答

你可以做如下事情。

str =  Z..M 

v1 = str[0,str.index( . )]
v2 = str[str.index( . )+2, str.length]

unless v1.to_i == 0
  v1 = v1.to_i
  v2 = v2.to_i
end

if v2>v1
  final_arr = (v1..v2).to_a
else
  final_arr = (v2..v1).to_a.reverse
end

puts final_arr

这既照顾到积极的范围,也照顾到不利幅度。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签