# If if if statment

**URL:** <https://rubytalk.org/t/if-if-if-statment/65348>\
**Category:** ruby-talk\
**Created:** [25 April 2012 07:17 UTC](https://rubytalk.org/t/if-if-if-statment/65348 "2012-04-25T07:17:32Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [25 April 2012 07:17 UTC](https://rubytalk.org/t/if-if-if-statment/65348/1 "2012-04-25T07:17:32Z")

</div>

Hi all,

if I take this code.

File.open("destination.txt", "w") do |out|  
&nbsp;&nbsp;File.foreach("original.txt") do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;out.puts line  
&nbsp;&nbsp;&nbsp;&nbsp;if line =~ /YOUR\_REGEX/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts "THE NEW LINE"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

it all works fine but if I do it like this

File.open("destination.txt", "w") do |out|  
&nbsp;&nbsp;File.foreach("original.txt") do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;out.puts line  
&nbsp;&nbsp;&nbsp;&nbsp;if line =~ /YOUR\_REGEX/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts "THE NEW LINE"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if line =~ /YOUR\_REGEX12/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts "THE NEW LINE12"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end  
end

It will only trigger on the first if statment.

I am trying to add say 10 different triggers and add a new line after  
that.

Regards  
/Fox

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [25 April 2012 07:32 UTC](https://rubytalk.org/t/if-if-if-statment/65348/2 "2012-04-25T07:32:46Z")

</div>

try this:

File.open("destination.txt", "w") do |out|  
File.foreach("original.txt") do |line|  
&nbsp;&nbsp;&nbsp;out.puts line  
&nbsp;&nbsp;&nbsp;if line =~ /YOUR\_REGEX/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts "THE NEW LINE"  
&nbsp;&nbsp;&nbsp;elsif line =~ /YOUR\_REGEX12/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts "THE NEW LINE12"  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

[see [http://www.howtogeek.com/howto/programming/ruby/ruby-if-else-if-command-syntax/\](http://www.howtogeek.com/howto/programming/ruby/ruby-if-else-if-command-syntax/%5C)]

martin

> **···**
>
> On Wed, Apr 25, 2012 at 12:17 AM, fox foxmaster \<lists@ruby-forum.com\> wrote:
> 
> > Hi all,
> > 
> > if I take this code.
> > 
> > File.open("destination.txt", "w") do |out|  
> > File.foreach("original.txt") do |line|  
> > out.puts line  
> > if line =~ /YOUR\_REGEX/  
> > out.puts "THE NEW LINE"  
> > end  
> > end  
> > end
> > 
> > it all works fine but if I do it like this
> > 
> > File.open("destination.txt", "w") do |out|  
> > File.foreach("original.txt") do |line|  
> > out.puts line  
> > if line =~ /YOUR\_REGEX/  
> > out.puts "THE NEW LINE"  
> > if line =~ /YOUR\_REGEX12/  
> > out.puts "THE NEW LINE12"  
> > end  
> > end  
> > end  
> > end
> > 
> > It will only trigger on the first if statment.
> > 
> > I am trying to add say 10 different triggers and add a new line after  
> > that.
> > 
> > Regards  
> > /Fox
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [25 April 2012 07:54 UTC](https://rubytalk.org/t/if-if-if-statment/65348/3 "2012-04-25T07:54:57Z")

</div>

Thanks Martin,

But when I do it with elsif it will take me one line further but then  
stop with error  
/pemi/Downloads/script/ruby/filemanipulate.rb:57:in `\<main\>'

filemanipulate.rb:71:in `block (2 levels) in \<main\>':  
undefined method `puts=' for #\<File:1234.bak (closed)\> (NoMethodError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filemanipulate.rb:58:in `foreach'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filemanipulate.rb:58:in `block in \<main\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filemanipulate.rb:57:in `open'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filemanipulate.rb:57:in `\<main\>'

/Fox

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [25 April 2012 08:09 UTC](https://rubytalk.org/t/if-if-if-statment/65348/4 "2012-04-25T08:09:49Z")

</div>

Hi I will attache the code since it gets format when I paste in paste  
bin

[http://pastebin.com/X5Cmkg5h](http://pastebin.com/X5Cmkg5h)

/Fox

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [25 April 2012 08:36 UTC](https://rubytalk.org/t/if-if-if-statment/65348/5 "2012-04-25T08:36:57Z")

</div>

One more that is nagging me is this

elsif line =~ /\<InterfaceGroup Name="BlacklistGroup" Members="/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts " \<\< vlan#{$vlan}\_#{$Client\_name},"

what I want to do here is not to make a new line instead I would like to  
insert it or append it to the line "BlacklistGroup" Members= insert  
here,"

/Fox

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Lars\_Haugseth1](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@Lars\_Haugseth1](https://rubytalk.org/u/Lars_Haugseth1)\
**Post date:** [25 April 2012 08:38 UTC](https://rubytalk.org/t/if-if-if-statment/65348/6 "2012-04-25T08:38:05Z")

</div>

Try using a case statement:

File.open("destination.txt", "w") do |out|  
&nbsp;&nbsp;&nbsp;File.foreach("original.txt") do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line2 = case line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /REGEX\_1/ then "NEW\_LINE\_1"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /REGEX\_2/ then "NEW\_LINE\_2"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /REGEX\_3/ then "NEW\_LINE\_3"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts line2 if line2  
&nbsp;&nbsp;&nbsp;end  
end

> **···**
>
> On 04/25/2012 09:17 AM, fox foxmaster wrote:
> 
> > File.open("destination.txt", "w") do |out|  
> > &nbsp;&nbsp;&nbsp;File.foreach("original.txt") do |line|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts line  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if line =~ /YOUR\_REGEX/  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts "THE NEW LINE"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if line =~ /YOUR\_REGEX12/  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts "THE NEW LINE12"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > It will only trigger on the first if statment.
> > 
> > I am trying to add say 10 different triggers and add a new line after  
> > that.
> 
> --
> 
> Lars Haugseth

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 April 2012 09:23 UTC](https://rubytalk.org/t/if-if-if-statment/65348/7 "2012-04-26T09:23:44Z")

</div>

Lars I thought I check out nokogiri ^^ and humm I feel completely lost,  
I am trying to find examples on how to use it in the way that I want, I  
really don't get the examples on the nokogiri web page,

So Lars or anyone else, would you like to show me howto use it in the  
code I have?

Regards

Fox

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 April 2012 07:48 UTC](https://rubytalk.org/t/if-if-if-statment/65348/8 "2012-04-25T07:48:42Z")

</div>

Another approach

RXS = {  
&nbsp;&nbsp;/YOUR\_REGEX/ =\> "THE NEW LINE",  
&nbsp;&nbsp;/YOUR\_REGEX12/ =\> "THE NEW LINE12",  
}

File.open("destination.txt", "w") do |out|  
&nbsp;&nbsp;File.foreach("original.txt") do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;out.puts line

&nbsp;&nbsp;&nbsp;&nbsp;RXS.each do |rx, add|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if rx =~ line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts add  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break # remove this if you want output for all triggers  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Kind regards

robert

> **···**
>
> On Wed, Apr 25, 2012 at 9:32 AM, Martin DeMello \<martindemello@gmail.com\> wrote:
> 
> > try this:
> > 
> > File.open("destination.txt", "w") do |out|  
> > File.foreach("original.txt") do |line|  
> > out.puts line  
> > if line =~ /YOUR\_REGEX/  
> > out.puts "THE NEW LINE"  
> > elsif line =~ /YOUR\_REGEX12/  
> > out.puts "THE NEW LINE12"  
> > end  
> > end  
> > end
> 
> > > I am trying to add say 10 different triggers and add a new line after  
> > > that.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 April 2012 08:00 UTC](https://rubytalk.org/t/if-if-if-statment/65348/9 "2012-04-25T08:00:34Z")

</div>

Please post the exact code you are using.

Cheers

robert

> **···**
>
> On Wed, Apr 25, 2012 at 9:54 AM, fox foxmaster \<lists@ruby-forum.com\> wrote:
> 
> > Thanks Martin,
> > 
> > But when I do it with elsif it will take me one line further but then  
> > stop with error  
> > /pemi/Downloads/script/ruby/filemanipulate.rb:57:in `\<main\>'
> > 
> > filemanipulate.rb:71:in `block (2 levels) in \<main\>':  
> > undefined method `puts=' for #\<File:1234.bak (closed)\> (NoMethodError)  
> > filemanipulate.rb:58:in `foreach'  
> > filemanipulate.rb:58:in `block in \<main\>
> > 
> > ```
> > filemanipulate\.rb:57:in \`open&#39;
> > filemanipulate\.rb:57:in \`&lt;main&gt;&#39;
> > 
> > ```
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [25 April 2012 08:13 UTC](https://rubytalk.org/t/if-if-if-statment/65348/10 "2012-04-25T08:13:21Z")

</div>

you are using

out.puts = "...."

when you want

out.puts "...."

martin

> **···**
>
> On Wed, Apr 25, 2012 at 1:09 AM, fox foxmaster \<lists@ruby-forum.com\> wrote:
> 
> > Hi I will attache the code since it gets format when I paste in paste  
> > bin
> > 
> > [http://pastebin.com/X5Cmkg5h](http://pastebin.com/X5Cmkg5h)
> > 
> > /Fox
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [25 April 2012 08:16 UTC](https://rubytalk.org/t/if-if-if-statment/65348/11 "2012-04-25T08:16:19Z")

</div>

Also, there are several ways you can clean your code up. Here's one  
quick fix: use %{....} as a string delimiter and you won't need to  
escape quotes within the string.

martin

> **···**
>
> On Wed, Apr 25, 2012 at 1:09 AM, fox foxmaster \<lists@ruby-forum.com\> wrote:
> 
> > Hi I will attache the code since it gets format when I paste in paste  
> > bin
> > 
> > [http://pastebin.com/X5Cmkg5h](http://pastebin.com/X5Cmkg5h)
> > 
> > /Fox
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Lars\_Haugseth1](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@Lars\_Haugseth1](https://rubytalk.org/u/Lars_Haugseth1)\
**Post date:** [25 April 2012 08:44 UTC](https://rubytalk.org/t/if-if-if-statment/65348/12 "2012-04-25T08:44:31Z")

</div>

Use a proper XML parser/writer, like Nokogiri: [http://nokogiri.org/](http://nokogiri.org/)

> **···**
>
> On 04/25/2012 10:36 AM, fox foxmaster wrote:
> 
> > One more that is nagging me is this
> > 
> > elsif line =~ /\<InterfaceGroup Name="BlacklistGroup" Members="/  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts "\<\< vlan#{$vlan}\_#{$Client\_name},"
> > 
> > what I want to do here is not to make a new line instead I would like to  
> > insert it or append it to the line "BlacklistGroup" Members= insert  
> > here,"
> 
> --  
> Lars Haugseth

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [25 April 2012 08:49 UTC](https://rubytalk.org/t/if-if-if-statment/65348/13 "2012-04-25T08:49:33Z")

</div>

Lars Haugseth wrote in post #1058261:

> > end
> > 
> > It will only trigger on the first if statment.
> > 
> > I am trying to add say 10 different triggers and add a new line after  
> > that.
> 
> Try using a case statement:
> 
> File.open("destination.txt", "w") do |out|  
> &nbsp;&nbsp;&nbsp;File.foreach("original.txt") do |line|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts line  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line2 = case line  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /REGEX\_1/ then "NEW\_LINE\_1"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /REGEX\_2/ then "NEW\_LINE\_2"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when /REGEX\_3/ then "NEW\_LINE\_3"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts line2 if line2  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> --
> 
> Lars Haugseth

Hi Lars,

That problem is actually solved but I agree that I need to look in to a  
xml parser but not for this projekt 🙂

This is my "new" prob  
elsif line =~ /\<InterfaceGroup Name="BlacklistGroup" Members="/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.puts " \<\< vlan#{$vlan}\_#{$Client\_name},"

what I want to do here is not to make a new line instead I would like to  
insert it or append it to the line "BlacklistGroup" Members= insert  
here,

/Fox

> **···**
>
> > On 04/25/2012 09:17 AM, fox foxmaster wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Lars\_Haugseth1](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@Lars\_Haugseth1](https://rubytalk.org/u/Lars_Haugseth1)\
**Post date:** [26 April 2012 10:49 UTC](https://rubytalk.org/t/if-if-if-statment/65348/14 "2012-04-26T10:49:29Z")

</div>

First, you need to parse the source XML into a DOM object:

&nbsp;&nbsp;&nbsp;require 'nokogiri'

&nbsp;&nbsp;&nbsp;dom = Nokogiri::XML(File.read source\_file)

Then you iterate over all InterfaceGroup nodes and modify attributes:

&nbsp;&nbsp;&nbsp;dom.xpath("//InterfaceGroup").each do |node|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node['Members'] = "some value"  
&nbsp;&nbsp;&nbsp;end

Finally, write the modified DOM to the target XML file:

&nbsp;&nbsp;&nbsp;File.open(target\_file, "w") { |f| f.write dom.to\_xml }

> **···**
>
> On 04/26/2012 11:23 AM, fox foxmaster wrote:
> 
> > Lars I thought I check out nokogiri ^^ and humm I feel completely lost,  
> > I am trying to find examples on how to use it in the way that I want, I  
> > really don't get the examples on the nokogiri web page,
> > 
> > So Lars or anyone else, would you like to show me howto use it in the  
> > code I have?
> 
> --  
> Lars Haugseth

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [25 April 2012 08:32 UTC](https://rubytalk.org/t/if-if-if-statment/65348/15 "2012-04-25T08:32:59Z")

</div>

Martin DeMello wrote in post #1058254:

> you are using
> 
> out.puts = "...."
> 
> when you want
> 
> out.puts "...."
> 
> martin

Oooh that was stupid of me ☹

Thanks for the tip and help Martin

Regards  
Fox

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Lars\_Haugseth1](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@Lars\_Haugseth1](https://rubytalk.org/u/Lars_Haugseth1)\
**Post date:** [25 April 2012 09:02 UTC](https://rubytalk.org/t/if-if-if-statment/65348/16 "2012-04-25T09:02:04Z")

</div>

Why not? It's evidently the proper tool for the job.

By all means, if you want to use a hammer to drive screws, go right ahead, but you won't get my opinion on the best way to do it.

> **···**
>
> On 04/25/2012 10:49 AM, fox foxmaster wrote:
> 
> > Hi Lars,
> > 
> > That problem is actually solved but I agree that I need to look in to a  
> > xml parser but not for this projekt 🙂
> 
> --  
> Lars Haugseth

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 April 2012 11:33 UTC](https://rubytalk.org/t/if-if-if-statment/65348/17 "2012-04-26T11:33:22Z")

</div>

> > Lars I thought I check out nokogiri ^^ and humm I feel completely lost,  
> > I am trying to find examples on how to use it in the way that I want, I  
> > really don't get the examples on the nokogiri web page,
> > 
> > So Lars or anyone else, would you like to show me howto use it in the  
> > code I have?

Just two nitpicks with regard to file handling:

> First, you need to parse the source XML into a DOM object:
> 
> require 'nokogiri'
> 
> dom = Nokogiri::XML(File.read source\_file)

# make Nokogiri read from the file to avoid to copy the whole file into memory  
# and read binary!  
dom = File.open(source\_file, 'rb') {|io| Nokogiri.XML(io)}

> Then you iterate over all InterfaceGroup nodes and modify attributes:
> 
> dom.xpath("//InterfaceGroup").each do |node|  
> node['Members'] = "some value"  
> end
> 
> Finally, write the modified DOM to the target XML file:
> 
> File.open(target\_file, "w") { |f| f.write dom.to\_xml }

# again avoid the copy and write binary  
File.open(target\_file, 'wb') { |io| dom.write\_to io }

Kind regards

robert

> **···**
>
> On Thu, Apr 26, 2012 at 12:49 PM, Lars Haugseth \<ruby-talk@larshaugseth.com\> wrote:
> 
> > On 04/26/2012 11:23 AM, fox foxmaster wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [27 April 2012 10:21 UTC](https://rubytalk.org/t/if-if-if-statment/65348/18 "2012-04-27T10:21:42Z")

</div>

Lars Haugseth wrote in post #1058449:

> First, you need to parse the source XML into a DOM object:
> 
> &nbsp;&nbsp;&nbsp;require 'nokogiri'
> 
> &nbsp;&nbsp;&nbsp;dom = Nokogiri::XML(File.read source\_file)
> 
> Then you iterate over all InterfaceGroup nodes and modify attributes:
> 
> &nbsp;&nbsp;&nbsp;dom.xpath("//InterfaceGroup").each do |node|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node['Members'] = "some value"  
> &nbsp;&nbsp;&nbsp;end
> 
> Finally, write the modified DOM to the target XML file:
> 
> &nbsp;&nbsp;&nbsp;File.open(target\_file, "w") { |f| f.write dom.to\_xml }

Hi Lars and thanks,

When I do this it will insert it like I want but is it also possible to  
make a newline like I did with the "old" variant?

Or is it better to first add the lines withe the old variant and then  
read that file with nokogiri and insert to the group lines?

Hope I can make myself understood.

Regards  
Fox

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Lars\_Haugseth1](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@Lars\_Haugseth1](https://rubytalk.org/u/Lars_Haugseth1)\
**Post date:** [27 April 2012 10:52 UTC](https://rubytalk.org/t/if-if-if-statment/65348/19 "2012-04-27T10:52:37Z")

</div>

You can use node.add\_child(new\_node), node.add\_next\_sibling(new\_node)  
etc. to insert new nodes into the DOM tree.

> **···**
>
> On 04/27/2012 12:21 PM, fox foxmaster wrote:
> 
> > When I do this it will insert it like I want but is it also possible to  
> > make a newline like I did with the "old" variant?
> 
> --  
> Lars Haugseth
