Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6073977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:19:52+00:00 2026-05-23T10:19:52+00:00

Regarding the following code: update: (thank you DGM and The Tin Man for recomendation

  • 0

Regarding the following code:

update:

(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
  • 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.

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:

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:19:53+00:00Added an answer on May 23, 2026 at 10:19 am

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

    it’s useless, you’re right

    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.

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

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

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

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

    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?

    Correct.

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

    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!

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

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

    Don’t understand this question.

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

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

    To conclude, it’s not beautiful code but it seems functional 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding the following code snippet I came across in one
Imagine I have the following code (simplified regarding my real context of course): <div
I have the following code, and i have a problem regarding changing the ringtone
I looking for a clarification regarding the pointers. I have compiled the following code
I have the following code reading a file containing unicode text (Japanese). File f
I have the following code which ends up forever reading '/proc/cpuinfo' as it keeps
I'm reading about sqlalchemy and I saw following code: employees_table = Table('employees', metadata, Column('employee_id',
I am reading a dataset with the following code from the directory: df= read.csv('101_E424690_N566610.csv',
I am reading javascript web application and the author is using following code: mod.load
With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.