Ruby:由空格分隔的2个字哈希键

时间:2016-03-19 03:47:03

标签: ruby hash

我的程序中缺少哪种方法可以使2个措辞状态像新泽西州和南卡罗来纳州等一样工作。我知道在单词之间添加下划线会更容易,但用户还需要在输入中包含下划线这不是很酷。我真的很感激任何帮助。谢谢!

us_capitals = { 
 "Alabama"=>"Montgomery", 
 "Alaska" => "Juneau", 
 "Arizona" => "Phoenix", 
 "Arkansas" => "Little Rock", 
 "California" => "Sacramento", 
 "Colorado" => "Denver", 
 "Connecticut" => "Hartford", 
 "Delaware" => "Dover", 
 "Florida" => "Tallahassee", 
 "Georgia" => "Atlanta", 
 "Hawaii" => "Honolulu", 
 "Idaho" => "Boise", 
 "Illinois" => "Springfield", 
 "Indiana" => "Indianapolis", 
 "Iowa" => "Des Moines", 
 "Kansas" => "Topeka", 
 "Kentucky" => "Frankfort",  
 "Louisiana" => "Baton Rouge", 
 "Maine" => "Augusta", 
 "Maryland" => "Annapolis", 
 "Massachusetts" => "Boston", 
 "Michigan" => "Lansing", 
 "Minnesota" => "Saint Paul",
 "Mississippi" => "Jackson", 
 "Missouri" => "Jefferson", 
 "Montana" => "Helena", 
 "Nebraska" => "Lincoln", 
 "Nevada" => "Carson City", 
 "New Hampshire" => "Concord", 
 "New Jersey" => "Trenton", 
 "New Mexico" => "Santa Fe", 
 "New York" => "Albany", 
 "North Carolina" => "Raleigh", 
 "North Dakota" => "Bismarck", 
 "Ohio" => "Columbus", 
 "Oklahoma" => "Oklahoma City", 
 "Oregon" => "Salem", 
 "Pennsylvania" => "Harrisburg", 
 "Rhode Island" => "Providence", 
 "South Carolina" => "Columbia", 
 "South Dakota" => "Pierre", 
 "Tennessee" => "Nashville", 
 "Texas" => "Austin", 
 "Utah" => "Salt Lake City", 
 "Vermont" => "Montpelier", 
 "Virginia" => "Richmond", 
 "Washington" => "Olympia",
 "West Virginia" => "Charleston", 
 "Wisconsin" => "Madison", 
 "Wyoming" => "Cheyenne"
}

loop do
  puts "Enter a state to lookup it's capital:"

  state = gets.chomp.capitalize

  puts us_capitals[state]
end

4 个答案:

答案 0 :(得分:1)

大写仅格式化字符串中的第一个字母,因此它不适用于包含多个单词的字符串。您可以尝试按空格分割字符串,将每个单词大写,然后将其加入:

private void AddAccount(String usernamenew, String passwordnew) {
    final String FileName = "F:/TextFiles/loginaccs.txt";
    File file = new File(FileName);

        try {
           // BufferedReader br = new BufferedReader(new FileReader(file));
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(usernamenew);
            bw.newLine();
            bw.write(passwordnew);
            bw.newLine();
            bw.close();

           // FileOutputStream fos = new FileOutputStream(file);      
           // fos.close();

           // br.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
}

Demonstration

如果您使用的是Rails,则可以使用state = gets.chomp.split(' ').map(&:capitalize).join(' ')

titleize

答案 1 :(得分:1)

假设gets返回"west virginIa\n"。然后

r = /[[:alpha:]]+/
state = gets.chomp.squeeze(' ').gsub(r) { |s| s.capitalize }
  #=> "west    virginIa\n".chomp.squeeze(' ').gsub(r) { |s| s.capitalize }
  #=> "west    virginIa".squeeze(' ').gsub(r) { |s| s.capitalize }
  #=> "west virginia".gsub(r) { |s| s.capitalize }
  #=> "West Virginia"

us_capitals[state]
  #=> "Charleston"

如果gets返回"West Virgin1a",我们会获得

state = gets.chomp.squeeze(' ').gsub(r) { |s| s.capitalize }
  #=> "West Virgin1a"
us_capitals[state]
  #=> nil

回想一下,String#capitalize将字符串的第一个字母转换为大写字母(如果是字母),但它也会将字符串中的所有其他字母转换为小写字母。

答案 2 :(得分:0)

您可以添加帮助方法来转换用户输入吗?

<div class="container large-margin-left large-margin-right">
    <div class="jcrop"><%= image_tag @photo.photo.large, :id=>'cropbox', :class=>'img-responsive' %></div>
    <br>
    <%= form_for [@user, @photo] do |f| %>
        <% for attribute in [:crop_x, :crop_y, :crop_h, :crop_w] %>
            <%= f.text_field attribute, :id=> attribute %>
        <% end %>
        <%= f.submit "Crop", :class=>'large-font pink-bg white caps sm-space', data: { disable_with: "Cropping..." } %>
    <% end %>
</div>
  • 在评论
  • 中归功于约旦

答案 3 :(得分:0)

似乎最简单的解决方案是使哈希键小写,然后downcase用户输入。

us_capitals = {
  # ...
  "nebraska" => "Lincoln", 
  "nevada" => "Carson City", 
  "new hampshire" => "Concord", 
  "new jersey" => "Trenton", 
  # ...
}

loop do
  puts "Enter a state to lookup it's capital:"

  state = gets.chomp.downcase

  puts us_capitals[state]
end