English 中文(简体)
我如何设法在鲁比拉进行比较? [复制]
原标题:How do I do String comparison in Ruby? [duplicate]
  • 时间:2010-01-06 20:57:08
  •  标签:
  • ruby

我正在使用以下代码比较插图,但总是将我带至<条码>。 为什么?

print("Enter your state abbreviation: ")
state_abbreviation = gets
if state_abbreviation.upcase == "NC"
  puts("North Carolina")
elsif state_abbreviation.upcase == "SC"
  puts("Sourth Carolina")
elsif state_abbreviation.upcase == "GA"
  puts("Georgia")
elsif state_abbreviation.upcase == "FL"
  puts("Florida")
elsif state_abbreviation.upcase == "AL"
  puts("Alabama")
else
  puts("You have enter wrong abbreviation")
end

我还尝试了<代码>.eql? (“string”),但我得出同样的结果。

最佳回答

靠车返回的扼杀行动最终将发生突破。 使用<代码>String#chomp去除(即:state_abbreviation=ccs.chomp)。

PS:如果你使用案件时间,而不是如果使用的话,那么你的代码会更清洁。

问题回答

我没有足够的意见,但我认为,克里斯·杰斯特-Young的教义是真的 ne。

statehash = { "RI" => "Rhode Island", "NC" => "North Carolina" }

print "Enter your state abbreviation: "
state_abbreviation = gets.chomp.upcase

puts statehash[state_abbreviation]

这部法典比一个小小小小小小小小小小小小小小小小小小小小小小。 它还允许 has国家物体,关键是缩略语,其价值为物体。

在主角之前,他说:

state_abbreviation.chomp!

作为使用<代码>case<>/code>的优异建议的一种替代方法,考虑将斜体与国家简称作为关键。

<代码>gets 回归你用新线分类的。 Try state_abbreviation =accesss.chomp

You are using gets, and probably your shell/input is adding a newline ( ) character at the end of the string, and maybe you may want to use the case statement:

例如:

print("Enter your state abbreviation: ") 
state_abbreviation = gets.strip

case state_abbreviation
  when "NC" then puts("North Carolina")
  when "SC" then puts("South Carolina")
  # ...
  else puts("You have enter wrong abbreviation")
end

虽然我们回避了专题,并对办法与问题进行了评论,但排除了这种铁路。 他从一个网站上抽出所有的国家缩略语。 我 m着这种狂热,因为它完全从你的法典中抽取了地图(不过,这或许是挽救网站的良好想法,就是如此)。 之后,你可以把这同一道,防止实际上袭击非行。

http://railscasts.com/episodes/179-seed-data

http://railscasts.com/episodes/115-caching-in-rails-2-1”rel=“nofollow” http://railscasts.com/episodes/115-caching-in-rails-2-1





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签