私は知らない言語でコードスニペットを私に残してしまって、それを維持しなければならないという不快な立場にいます。私は自分自身にRubyを導入していませんが、その一部は非常にシンプルですが、それでもあなたの説明を聞きたいと思います。 は、ここに行く:Rubyコードスニペットを説明する
words = File.open("lengths.txt") {|f| f.read }.split # read all lines of a file in 'words'?
values = Array.new(0)
words.each { |value| values << value.to_i } # looked this one up, it's supposed to convert to an array of integers, right?
values.sort!
values.uniq!
diffs = Array.new(0) # this looks unused, unless I'm missing something obvious
sum = 0
s = 0 # another unused variable
# this looks like it's computing the sum of differences between successive
# elements, but that sum also remains unused, or does it?
values.each_index { |index| if index.to_i < values.length-1 then sum += values.at(index.to_i + 1) - values.at(index.to_i) end } # could you also explain the syntax here?
puts "delta has the value of\n"
# this will eventually print the minimum of the original values divided by 2
puts values.at(0)/2
上記のスクリプトは、リスト内の各2つの連続した要素(整数、本質的)間の差の平均を把握することになりました。私はこれが本当に何かの近くにないと言っているのでしょうか?それともRubyの知識がないと考えているような根本的なものを見逃していますか?
うわー、それは私がそれを見た場合包括的な答えです!そして、ええ、Rubyはかなり強力に見えます。 –