解释Ruby代码片段

时间:2011-01-08 11:23:52

标签: 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

上面的脚本应该计算出列表中每两个连续元素(本质上是整数)之间的差异的平均值。我是否正确地说这远不是它实际上做的,或者我错过了一些基本的东西,这可能是因为我没有Ruby知识?

3 个答案:

答案 0 :(得分:9)

解释+重构(删除未使用的变量,功能方法,each_cons):

# Read integer numbers from file, sort them ASC and remove duplicates
values = File.read("lengths.txt").split.map(&:to_i).sort.uniq

# Take pairwise combinations and get the total sum of partial differences
partial_diffs = values.each_cons(2).map { |a, b| b - a }.inject(0, :+)

答案 1 :(得分:7)

那个人肯定没有抓住Ruby自己。我想知道为什么他选择使用那种语言。

这是一个带注释的解释:

# Yes, it reads all lines of a file in words (an array)
words = File.open("lengths.txt") {|f| f.read }.split

values = Array.new(0)
# Yes, to_i convert string into integer
words.each { |value| values << value.to_i }
values.sort!
values.uniq!

# diffs and s seem unused
diffs = Array.new(0)
sum = 0
s = 0

# The immediate line below can be read as `for(int index = 0; index < values.length; index++)`
values.each_index { |index|
    # index is integer, to_i is unnecessary
    if index.to_i < values.length-1 then

        # The `sum` variable is used here
        # Following can be rewritten as sum += values[i-1] - values[i]
        sum += values.at(index.to_i + 1) - values.at(index.to_i)
    end
}

puts "delta has the value of\n"

# Yes, this will eventually print the minimal of the original values divided by 2
puts values.at(0) / 2

为了帮助您更好地掌握Ruby的“真实”(惯用),我编写了您想要的内容,并附带了一些注释

values = open("lengths.txt") do |f|
    # Read it like this:
    #
    # Take the list of all lines in a file,
    # apply a function to each line
    # The function is stripping the line and turning it 
    # into an integer
    # (This means the resultant list is a list of integers)
    #
    # And then sort it and unique the resultant list
    #
    # The eventual resultant list is assigned to `values`
    # by being the return value of this "block"
    f.lines.map { |l| l.strip.to_i }.sort.uniq
end

# Assign `diffs` to an empty array (instead of using Array.new())
diffs = []
values.each_index do |i|
    # Syntactic sugar for `if`
    # It applies the 1st part if the 2nd part is true
    diffs << (values[i+1] - values[i]) if i < values.length - 1
end

# You can almost read it like this:
#
# Take the list `diffs`, put all the elements in a sentence, like this 
# 10 20 30 40 50
#
# We want to inject the function `plus` in between every element, 
# so it becomes
# 10 + 20 + 30 + 40 + 50
#
# The colon `:+` is used to refer to the function `plus` as a symbol
#
# Take the result of the above summation, divided by length,
# which gives us average
delta = diffs.inject(:+) / diffs.length

# `delta` should now contains the "average of differences" between 
# the original `values`

# String formatting using the % operator
# No \n needed since `puts` already add one for us
puts "delta has the value of %d" % delta

这绝不是推动Ruby的真正力量,但你明白为什么Rubyists如此热衷于表达性和东西:P

答案 2 :(得分:1)

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 } 

上述行总结了连续值之间的差异。由于index.to_i < values.length-1,测试values.at(index.to_i + 1)不会超出范围访问数组。

你是对的,这段代码做的不多。它只打印文件中最小值的一半。