【问题标题】:alternate uppercase and lowercase ruby交替大写和小写红宝石
【发布时间】:2015-11-08 23:30:23
【问题描述】:

我对 Ruby 很陌生,想知道如何自动化一个空数组来显示输入的数据以按字母顺序排序,并在大写和小写之间交替每个条目。我在这个网站上看到过类似的帖子,描述了如何实现这一点,但似乎无法让它发挥作用。

以下是我的代码,这里使用的是示例:How to have the user input alternate between uppercase and lowercase in Ruby?

目前我的代码首先交替大写和小写,然后按字母顺序排序(创建两个列表)。我想要的只是一个按字母顺序排列并交替大小写的列表。我希望这是有道理的,谢谢你的时间!

puts "Welcome to the word list!"

words = []

5.times do
    puts "Please enter a word:"
    words << gets.chomp
end    

words.each_with_index do |word, index|
    if index.even?
      puts word.upcase
    else
      puts word.downcase
    end
end

puts "Here are your words:"
puts words.sort

【问题讨论】:

    标签: ruby


    【解决方案1】:

    首先,这两种情况你会得到不同的结果:

     1. sort the words first and then apply your alternate upcase/downcase logic.
     2. apply your alternate upcase/downcase logic first, then sort the words.
    

    因为如果先对单词进行排序,则原始输入数组中单词的索引会发生变化。因此,当您根据输入数组中单词的索引执行此操作时,它将对您的大写/小写逻辑产生影响。

    所以,要先对单词进行排序还是最后排序,这取决于您的原始要求。

    这是第一种方法:

    puts "Welcome to the word list!"
    
    words = []
    results = []
    
    5.times do
      puts "Please enter a word:"
      words << gets.chomp
    end
    
    words.sort.each_with_index do |word, index| # sort the input words first, then loop through
      # accumulate the results in the results array
      if index.even?
        results << word.upcase
      else
        results << word.downcase
      end
    end
    
    puts "Here are your resulting words: "
    puts results
    

    如果你想要第二个,你可以在最后进行排序。

    【讨论】:

    • 非常感谢您的回答,效果很好。很抱歉再问一个问题,我如何让“结果词”在没有括号和引号的情况下列出并将它们放在单独的行上?谢谢!
    • @garfield_digital 只需:puts results 而不是 puts results.inspect
    • 欢迎 :) 您应该接受您最喜欢的答案并找到最适合您的问题的答案。详情见此贴:meta.stackexchange.com/questions/5234/…
    【解决方案2】:

    我会这样做:

    enum = [:upcase, :downcase].cycle
      #<Enumerator: [:upcase, :downcase]:cycle>
    
    5.times.with_object([]) do |_,words|
      puts "Please enter a word:"
      words << gets.chomp.send(enum.next)
    end
    

    如果输入了"dog\n""cat\n""pig\n""cow\n""owl\n",则会返回:

    ["DOG", "cat", "PIG", "cow", "OWL"]
    

    由于您是 Ruby 新手,如果您理解我在第一次阅读时所做的事情,我会感到惊讶。但是,如果您将其分解,则还不错。如果你一直努力直到你理解它,我保证你会在你对语言的理解上有很大的飞跃。

    如果您查看 Array#cycle(返回类 Enumerator 的实例)和 Enumerator#next 的文档,您会发现:

    enum.next #=> :upcase
    enum.next #=> :downcase
    enum.next #=> :upcase
    enum.next #=> :downcase
    ...
    

    将方法分派给接收者是Object#send 的工作。每当您调用方法(比如[1,2] &lt;&lt; 3)时,Ruby 都会将方法(名称)及其参数发送给接收者([1,2].send(:&lt;&lt;, 3)。

    所以你看,方法名:upcase被发送给所有 偶数字和:downcase 被发送到所有奇数字。

    Enumerator#with_object 方法是一种更类似于 Ruby 的生成结果的方式:

    words = []
    5.times do
      puts "Please enter a word:"
      words << gets.chomp.send(enum.next)
    end
    words
    

    注意with_object 节省了两个步骤,但它还有一个优点是将数组words 的范围限制在块中。

    一个相关的方法,Enumerable#each_with_object可以用来代替each_object的(因为类EnumeratorincludeEnumerable模块)并且必须在接收者不是枚举器时使用(例如,当接收者是一个数组或散列时)。

    最后,您可以将块变量写为|i,words|,其中i 是索引(由Integer#times 生成),范围从04。通常的做法是用下划线替换块中未使用的块变量,部分原因是为了通知读者。请注意,_ 是一个完全合法的局部变量。

    【讨论】:

    • 非常感谢您的详细回答,这在我的脑海中,但非常有帮助和大开眼界。我会花时间研究这个,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多