Ruby将字符串转换为整数

时间:2015-03-31 18:18:29

标签: ruby arrays string sorting integer

我试图在数组中转换字符串



values = (1..10).to_a + ["Jack(11)", "Queen(12)", "King(13)", "Ace(14)"]




成一个整数,然后用

对数组进行排序



def straight(hand)
  numbers = hand.each { |card| card.to_i }
  numbers.sort
end




我不断收到错误



["Jack(11)", 9, 10, "Queen(12)", "Ace(14)"]
5carddraw.rb:58:in `sort': comparison of String with 10 failed (ArgumentError)




谁能告诉我如何才能做到这一点?

2 个答案:

答案 0 :(得分:1)

这里有一些误解。

每个

each遍历数组,将每个元素传递给一个块,然后返回数组本身。因此each不会对数组进行任何更改(除非您明确指出)。由于您的广告代码只调用to_i,因此最终结果为numbers == hand

ary = ["Jack(11)", "Queen(12)", "King(13)", "Ace(14)"]
tmp = ary.each { |card| card.to_i }
p tmp
# ["Jack(11)", "Queen(12)", "King(13)", "Ace(14)"]

因此,您最终会对包含数字和字符串的数组进行排序,这就是您的错误所抱怨的内容。您想使用map,它使用块的结果返回一个新数组。

to_i

to_i执行从字符串到整数的非常简单的转换:如果字符串以某些数字开头(忽略空格),则会从这些数字中生成一个数字。如果字符串不以数字开头,则返回0:

" 1 2".to_i       # 1
"99bottles".to_i  # 99
"match3".to_i     # 0

由于您的数字位于字符串的中间,因此需要更高级的内容(如regexp)来提取它们:

"Jack(11)"[/\d+/].to_i # 11

答案 1 :(得分:0)

编辑我已经将这些长篇文章包装起来,以使这篇文章易于阅读,必须复制并粘贴到irb中才能阅读。抱歉。

 values = ("1".."10").to_a + ["Jack(11)", "Queen(12)", "King(13)",  "Ace(14)"]

返回:

["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack(11)", 

"Queen(12)", "King(13)", "Ace(14)"]

但是values.sort会给出:

["1", "10", "2", "3", "4", "5", "6", "7", "8", "9", "Ace(14)",
 "Jack(11)", "King(13)", "Queen(12)"]

所以你可以这样做:

values = ("01".."10").to_a + ["Jack(11)", "Queen(12)", "King(13)", "Ace(14)"]

将返回:

["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", 

"Jack(11)", "Queen(12)", "King(13)", "Ace(14)"]

但仍然是values.sort给出:

["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "Ace(14)",

"Jack(11)", "King(13)", "Queen(12)"]

所以最后你可以做到:

values = ("01".."10").to_a + ["(11)Jack", "(12)Queen", "(13)King", "(14)Ace"]

给出:

["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", 

 "(11)Jack", "(12)Queen", "(13)King", "(14)Ace"]

所以你试试values.sort并得到......

["(11)Jack", "(12)Queen", "(13)King", "(14)Ace", "01", "02", "03", 

"04", "05", "06", "07", "08", "09", "10"]

Darn it!还是行不通。这一切告诉你什么?您尝试使用的阵列是错误的数据结构。使用哈希并停止尝试对任何内容进行排序:

values = {one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7,

 eight: 8, nine: 9, ten: 10, jack: 11, queen: 12, king: 13, ace: 14}

def straight(hand)
  numbers = hand.values.sort
end 

straight(values)

  #returns => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

查看Hash和Enumerable类中的所有方法。这些将为您提供处理卡值的哈希的巨大武器库。这些方法中的许多方法都会做你已经做过的事情,或者可能会做更多的代码。

在我考虑这个问题时,我会创建一个Hash作为我的字典,使用字符串而不是我上面显示的书写数字:

Values = {"1" => 1, "2" => 2, "3" => 3, "4" => 4, "5" => 5, "6" => 6,
 "7" => 7, "8" => 8, "9" => 9, "10" => 10, "Jack" => 11, "Queen" => 12,
 "King" => 13, "Ace" => 14} 

#note we are using a Constant now with "Values". We want to access this anywhere and for it to not change.

然后您可以使用显示卡片的字符串,但是当您想要查找手的值时,可以将其转换为数字数组:

hand = ["2", "4", "10", "Ace", "King"]
def hand_value(hand)
  hand.map { |card| Values[card]}
end
    # => [2, 4, 10, 14, 13]

现在你可以以任何你想要的方式操纵手的价值。您可以检查它们是否是连续的,总结它们等等。

查找值:

Values["King"] #=> 13

反过来查找一个键:

Values.key(13) #=> "King"