如何遍历此Ruby对象?

时间:2018-05-10 04:13:56

标签: ruby nokogiri

我正在使用Oga gem,我这样做了:

@output   = document.xpath('//li/a')

产生了这个输出:

> #<GrabFeedFromJSE:0x00007fe7098bb648 @titles=NodeSet(Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/jse-index")] children: NodeSet(Text("\n\n                \n\n\n                JSE Index \n                \n                "), Element(name: "br"), Text("\n\n                4,614,305 Vol\n                "), Element(name: "br"), Text("\n\n                                299,175.87                \n                "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n                \n                -541.06\n                            "))), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/all-jamaican")] children: NodeSet(Text("\n\n                \n\n\n                JSE All Jamaican Composite Index \n                \n                "), Element(name: "br"), Text("\n\n                3,911,832 Vol\n                "), Element(name: "br"), Text("\n\n                                328,363.09                \n                "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n                \n                -593.84\n                            "))), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/jse-select")] children: NodeSet(Text("\n\n                \n\n\n                JSE Select Index \n                \n                "), Element(name: "br"), Text("\n\n                3,036,580 Vol\n                "), 

我不知道如何实际遍历这个对象。

我尝试过普通的Ruby方法(对于Arrays,Hashes等)并且它们不起作用。我甚至为NodeSet object in Nokogiri尝试了那些但是没有用...例如:

>>  @output.titles
NoMethodError: undefined method `titles' for #<GrabFeedFromJSE:0x00007fe7098bb648>
    from /app/views/portfolio/ticker.html.erb:5:in `_app_views_portfolio_ticker_html_erb__2987784693093146087_70315110554280'
>>  @output[:titles]
NoMethodError: undefined method `[]' for #<GrabFeedFromJSE:0x00007fe7098bb648>
    from /app/views/portfolio/ticker.html.erb:5:in `_app_views_portfolio_ticker_html_erb__2987784693093146087_70315110554280'
>>  @output.at_xpath('a')
NoMethodError: undefined method `at_xpath' for #<GrabFeedFromJSE:0x00007fe7098bb648>
    from /app/views/portfolio/ticker.html.erb:5:in `_app_views_portfolio_ticker_html_erb__2987784693093146087_70315110554280'

如何访问该对象中的数据?

1 个答案:

答案 0 :(得分:2)

您的输出会告诉您有<ion-input type="text" [(ngModel)]="passenger['email']"></ion-input> 的实例:

GrabFeedFromJSE

您必须查找其文档才能知道如何访问它。

如果你只是想知道它有哪些方法,你可以使用Ruby反射来做到这一点,例如

#<GrabFeedFromJSE:0x00007fe7098bb648 @titles=NodeSet(Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/jse-index")] children: NodeSet(Text("\n\n                \n\n\n                JSE Index \n                \n                "), Element(name: "br"), Text("\n\n                4,614,305 Vol\n                "), Element(name: "br"), Text("\n\n                                299,175.87                \n                "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n                \n                -541.06\n                            "))), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/all-jamaican")] children: NodeSet(Text("\n\n                \n\n\n                JSE All Jamaican Composite Index \n                \n                "), Element(name: "br"), Text("\n\n                3,911,832 Vol\n                "), Element(name: "br"), Text("\n\n                                328,363.09                \n                "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n                \n                -593.84\n                            "))), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/jse-select")] children: NodeSet(Text("\n\n                \n\n\n                JSE Select Index \n                \n                "), Element(name: "br"), Text("\n\n                3,036,580 Vol\n                "), 
  ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

>>  @output.titles
NoMethodError: undefined method `titles' for #<GrabFeedFromJSE:0x00007fe7098bb648>
                                               ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
  from /app/views/portfolio/ticker.html.erb:5:in `_app_views_portfolio_ticker_html_erb__2987784693093146087_70315110554280'
>>  @output[:titles]
NoMethodError: undefined method `[]' for #<GrabFeedFromJSE:0x00007fe7098bb648>
                                           ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
  from /app/views/portfolio/ticker.html.erb:5:in `_app_views_portfolio_ticker_html_erb__2987784693093146087_70315110554280'
>>  @output.at_xpath('a')
NoMethodError: undefined method `at_xpath' for #<GrabFeedFromJSE:0x00007fe7098bb648>
                                                 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
  from /app/views/portfolio/ticker.html.erb:5:in `_app_views_portfolio_ticker_html_erb__2987784693093146087_70315110554280'
>> @output[0].path
NoMethodError: undefined method '[]' for #<GrabFeedFromJSE:0x00007fe7098bb648>
                                           ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

等等。

注意:您使用的是完全不同的XML处理器,因此使用Nokogiri的文档不会对您有所帮助。您需要查看Oga的文档。但是,您正在处理的对象不是Oga对象,而是@output.methods # to get a list of methods m = @output.method(:foo) # to get a specific method m.owner # to find out which module the method belongs to 对象。如果没有文档,您应查阅其文档或与其作者联系。