如何理解 Ruby 中的数组和循环? [英] How to understand this Arrays and loops in Ruby?

查看:75
本文介绍了如何理解 Ruby 中的数组和循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于以下代码:

(感谢 DGM 和铁皮人对代码的推荐和解释.)

(thank you DGM and The Tin Man for recomendation on code and apneadiving for explanation.)

#################
# get main page
#################
  rows = doc.xpath('//table[@class="articulos"]/tr[td[5]/p/b]')
  i = 0
  details = rows.each do |row|
    detail = {}  
    [
      [:sku, 'td[3]/text()'],
      [:desc, 'td[4]/text()'],
      [:stock, "td[5]/p[@title]"],
      [:price, 'td[6]/text()']
    ].each do |name, xpath|
        detail[name] = row.at_xpath(xpath).to_s.strip

      end
    i = i + 1
    if detail[:sku] != ""
          price = detail[:price].split

          if price[1] == "D"
              currency = 144
          else
              currency = 168
          end
          stock = detail[:stock].gsub(/[^\d]/, '')
          cost = price[0].gsub(",", "").to_f
  end

  • 第一个 i = 0, i = i + 1 是必要的吗?
  • 这是使用什么语法?details = rows.each do |row|
  • 为什么要使用 detail = {}?这是在做什么?
  • 我确实理解 .each do |name,xpath| 因为它的顺序是:name, xpath.
  • 我猜 detail[name] = row.at_xpath(xpath).to_s.strip 是说,如果我调用 detail[:sku],它会让xpath 处的那一行到字符串条.
    • Is the first i = 0, i = i + 1 neccesary?
    • What syntax is this using? details = rows.each do |row|
    • Why would you want to use detail = {}? What is this doing?
    • I do understand .each do |name,xpath| because it is in the order: name, xpath.
    • I guess detail[name] = row.at_xpath(xpath).to_s.strip is saying that, if I call detail[:sku], it will make that row at that xpath to a string strip.
    • 对于阅读 Ruby Poignant Book 阅读我上面写的代码后的理解可以翻译成 Ruby 逻辑词吗?如果这不是在侮辱 Ruby 专家,哈哈.

      for what I understand after reading Ruby Poignant Book reading the code I wrote above I can maybe translate to Ruby logic words?. If that is not to insulting for Ruby Experts,haha.

      首先,我们有一个方法的循环,在另一个方法中包含一个数组.

      First we have a loop of one method, with an array of arrays, inside another one.

      variable = variable.method block |block argument|
        variable = {block}
        [ array of arrays
          [ symbol_1, 'string'],
          [ symbol_2, 'string'],
          [ symbol_3, 'string'],
          [ symbol_4, 'string'],
          [ symbol_5, 'string'],
          [ symbol_6, 'string']
        ].method block |symbol, string|
          variable[symbol] = variable.method(method argument).method.kernel_method
        end block
      end block
      

      ???这样对吗?现在我需要解释一下在代码中使用方法、变量和参数的实际名称,让我们看看:

      ??? is this correct? now I need to explain that using the method , variable, and arguments actual names in the code, lets see:

      rows 变量获取一个 collect 消息以收集包含符号和 xpath 的数组数组中的一行,并且对于每个数组块,行和 xpath 将应用内核方法条带?

      The rows variable gets a collect message to collect a row of an array of arrays containing the symbol and xpath and for each block of arrays the row and xpath wil be applied a kernel method strip?

      推荐答案

      第一个 i = 0, i = i + 1 (为什么是这里有必要吗?)

      The first i = 0, i = i + 1 (why is that neccesary here?)

      没用,你说得对

      details = rows.collect 做 |row|什么语法是为了什么?一个数组?还是什么班级是从 nokogiri 使用的?做nokogiri 跟这个有关系或者只是 RUBY 处理对象.

      details = rows.collect do |row| what syntax is this for? a array? or what class is using from nokogiri? does nokogiri has anything to do with this or is it only RUBY handling an object.

      这是纯粹的 ruby​​ 风格,它循环了应该是数组的元素.

      it's sheer ruby style, it loops the elements of what should be an Array.

      它还重新创建了一个存储在 details 中的数组,其中包含在循环结束时声明的每个元素.这是detail

      It also recreates an Array stored in details containing every element declared at the end of the loop. Here it's detail

      detail = {} 你看到了.为什么会你想用这个吗?这是在做什么我没有看到什么?

      detail = {} you see this. why would you want to use this? is this doing something I am not seing?

      它将detail 初始化为一个空的Hash,然后填充到collect 循环中.

      It initializes detail to an empty Hash which is then filled in the collect loop.

      我明白 .collect 做|名称,xpath|因为顶部在名称顺序,xpath detail[name] =row.at_xpath(xpath).to_s.strip 我猜这是说如果我打电话detail[:sku] 它将使该行位于字符串条的xpath?

      I do understand .collect do |name,xpath| because of the top is in order in name , xpath detail[name] = row.at_xpath(xpath).to_s.strip I guess this is saying that if I call detail[:sku] it will make that row at that xpath to a string strip?

      正确.

      .map 有什么作用?有没有语法nokogiri 页面中的教程?

      what does .map do? is there a syntax tutorial in nokogiri page for this?

      map 循环应该是哈希的所有元素.它创建一个包含链接的数组.多亏了 uniq!

      map loops all the elements of what should be a Hash. It creates an Array containing links. Duplicates of links are removed from this Array thanks to uniq!

      注意这个函数末尾的 bang,表示它改变了对象.

      Notice the bang at then end of this function, it means it changes the object.

      为什么他必须重复所有数据在 #walk 槽分页器中只是使用 idk 一些变量?

      and why did he had to repeat all data in the #walk trough paginator instead of just using idk some variable?

      不明白这个问题.

      细节<<详细我知道这叫推?那么为什么有人想要推动一些同名哈哈...

      detail << detail I know this is called a push? so why do anyone want to push some same name haha...

      这是一个推动,但它是details <<细节所以逻辑是安全的.

      It's a push but it's details << detail so logic is safe.

      总而言之,这不是漂亮的代码,但看起来很实用:)

      To conclude, it's not beautiful code but it seems functional :)

      这篇关于如何理解 Ruby 中的数组和循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆