Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2800 > unrolled thread
| Started by | Ted Flethuseo <flethuseo@gmail.com> |
|---|---|
| First post | 2011-04-13 19:06 -0500 |
| Last post | 2011-04-14 12:34 -0500 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.ruby
removing nodes from Nokogiri::XML::NodeSet Ted Flethuseo <flethuseo@gmail.com> - 2011-04-13 19:06 -0500
Re: removing nodes from Nokogiri::XML::NodeSet Mike Dalessio <mike.dalessio@gmail.com> - 2011-04-13 22:02 -0500
Re: removing nodes from Nokogiri::XML::NodeSet Ted Flethuseo <flethuseo@gmail.com> - 2011-04-14 08:31 -0500
Re: removing nodes from Nokogiri::XML::NodeSet Robert Klemme <shortcutter@googlemail.com> - 2011-04-14 09:40 -0500
Re: removing nodes from Nokogiri::XML::NodeSet 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-14 12:34 -0500
| From | Ted Flethuseo <flethuseo@gmail.com> |
|---|---|
| Date | 2011-04-13 19:06 -0500 |
| Subject | removing nodes from Nokogiri::XML::NodeSet |
| Message-ID | <124883eae0d1e5dcae64297d4872ee9d@ruby-forum.com> |
Hi everyone, I am having trouble removing a node from a Nokogiri::XML::NodeSet, not sure what I should use to remove a specific node at a given position. I have tried the following but it doesn't seem to work: puts nodes.length nodes[0].remove puts nodes.length Ted -- Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Mike Dalessio <mike.dalessio@gmail.com> |
|---|---|
| Date | 2011-04-13 22:02 -0500 |
| Message-ID | <BANLkTimxRrSMSR9L-NDN54SfYbX+uQ9+CA@mail.gmail.com> |
| In reply to | #2800 |
[Note: parts of this message were removed to make it a legal post.] Greetings, On Wed, Apr 13, 2011 at 8:06 PM, Ted Flethuseo <flethuseo@gmail.com> wrote: > Hi everyone, > > I am having trouble removing a node from a Nokogiri::XML::NodeSet, not > sure what > I should use to remove a specific node at a given position. > Nokogiri's NodeSet implements Enumerable, and much of the Array interface, so you should be able to treat it much like you'd treat any Ruby Array. For example, to remove the first node in a NodeSet, simply use `nodes.shift`. To remove the last node, use `nodes.pop`. Etc. > I have tried the following but it doesn't seem to work: > > puts nodes.length > nodes[0].remove > Just a note, that the above code removes the first node in the NodeSet from its document. Not what you want. Best of luck.
[toc] | [prev] | [next] | [standalone]
| From | Ted Flethuseo <flethuseo@gmail.com> |
|---|---|
| Date | 2011-04-14 08:31 -0500 |
| Message-ID | <376d66f2397654d611943c6d3109c4b4@ruby-forum.com> |
| In reply to | #2816 |
It doesn't remove it though. I also don't want specifically to remove the first or last node, often I want to remove one somewhere in the middle of the array. puts nodes.length nodes[0].remove puts nodes.length when I run that code I have originally 4 nodes, and after, I still have 4 nodes. I found a way to do this but I disklike it, because it has to search for the node every time it needs to erase it. puts nodes.length nodes.delete(nodes[0]) puts nodes.length Ted Mike Dalessio wrote in post #992653: > Greetings, > > On Wed, Apr 13, 2011 at 8:06 PM, Ted Flethuseo <flethuseo@gmail.com> > wrote: > >> Hi everyone, >> >> I am having trouble removing a node from a Nokogiri::XML::NodeSet, not >> sure what >> I should use to remove a specific node at a given position. >> > > Nokogiri's NodeSet implements Enumerable, and much of the Array > interface, > so you should be able to treat it much like you'd treat any Ruby Array. > > For example, to remove the first node in a NodeSet, simply use > `nodes.shift`. To remove the last node, use `nodes.pop`. Etc. > > >> I have tried the following but it doesn't seem to work: >> >> puts nodes.length >> nodes[0].remove >> > > Just a note, that the above code removes the first node in the NodeSet > from > its document. Not what you want. > > Best of luck. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-04-14 09:40 -0500 |
| Message-ID | <BANLkTikAfT86EJm-yobHRm+BELPwzhV8Tg@mail.gmail.com> |
| In reply to | #2844 |
On Thu, Apr 14, 2011 at 3:31 PM, Ted Flethuseo <flethuseo@gmail.com> wrote:
> It doesn't remove it though. I also don't want specifically to remove
> the first or last node, often I want to remove one somewhere in the
> middle of the array.
>
> puts nodes.length
> nodes[0].remove
> puts nodes.length
>
> when I run that code I have originally 4 nodes, and after, I still have
> 4 nodes. I found a way to do this but I disklike it, because it has to
> search for the node every time it needs to erase it.
>
> puts nodes.length
> nodes.delete(nodes[0])
> puts nodes.length
You want method #remove:
irb(main):006:0> doc =
Nokogiri::XML("<root><test>1</test><test>2</test></root>")
=> #<Nokogiri::XML::Document:0x832a9b2 name="document"
children=[#<Nokogiri::XML::Element:0x832a728 name="root"
children=[#<Nokogiri::XML::Element:0x8328c20 name="test"
children=[#<Nokogiri::XML::Text:0x8328a9a "1">]>,
#<Nokogiri::XML::Element:0x8328914 name="test"
children=[#<Nokogiri::XML::Text:0x832878e "2">]>]>]>
irb(main):007:0> puts doc
<?xml version="1.0"?>
<root>
<test>1</test>
<test>2</test>
</root>
=> nil
irb(main):008:0> doc.xpath('//test')
=> [#<Nokogiri::XML::Element:0x8328c20 name="test"
children=[#<Nokogiri::XML::Text:0x8328a9a "1">]>,
#<Nokogiri::XML::Element:0x8328914 name="test"
children=[#<Nokogiri::XML::Text:0x832878e "2">]>]
irb(main):009:0> doc.xpath('//test[last()]')
=> [#<Nokogiri::XML::Element:0x8328914 name="test"
children=[#<Nokogiri::XML::Text:0x832878e "2">]>]
irb(main):010:0> doc.xpath('//test[last()]').remove
=> [#<Nokogiri::XML::Element:0x8328914 name="test"
children=[#<Nokogiri::XML::Text:0x832878e "2">]>]
irb(main):011:0> puts doc
<?xml version="1.0"?>
<root>
<test>1</test>
</root>
=> nil
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-14 12:34 -0500 |
| Message-ID | <710026eb14181b797131cfc98b12e535@ruby-forum.com> |
| In reply to | #2800 |
Ted Flethuseo wrote in post #992628: > Hi everyone, > > I am having trouble removing a node from a Nokogiri::XML::NodeSet, not > sure what > I should use to remove a specific node at a given position. > > I have tried the following but it doesn't seem to work: > > puts nodes.length > nodes[0].remove > puts nodes.length > > > Ted Consider this example: str = "hello \n world" arr = str.split " " p arr --output:-- ["hello", "world"] str.delete!(arr[0]) puts str puts arr.length -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web