chell
chell

Reputation: 7866

remove_attribute returning nil node element instead of just deleting attribute nokogiri

I am trying to remove an attribute from a Nokogiri node element.

Here is the first node element:

=> #(Element:0x3fed0eaf2ef0 {
  name = "ins",
  namespace = #(Namespace:0x3fed0ed24408 { prefix = "w", href = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }),
  attributes = [
    #(Attr:0x3fed0eb1e528 { name = "id", namespace = #(Namespace:0x3fed0ed24408 { prefix = "w", href = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }), value = "0" }),
    #(Attr:0x3fed0eb1e514 { name = "author", namespace = #(Namespace:0x3fed0ed24408 { prefix = "w", href = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }), value = "Mitchell Gould" })
    #(Attr:0x3fed0eb1e500 { name = "date", namespace = #(Namespace:0x3fed0ed24408 { prefix = "w", href = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }), value = "2016-10-15T14:43:00Z" })]
  })

When I do:

   first = all_ins_nodes.first.remove_attribute("name")

first => nil

I only want to remove the attribute not delete the entire node element. How can I do this?

Upvotes: 0

Views: 186

Answers (1)

max pleaner
max pleaner

Reputation: 26758

Right here

first = all_ins_nodes.first.remove_attribute("name")

should be split up into multiple lines

first = all_ins_node.first
first.remove_attribute("name")

The reason is that .remove_attribute evidently returns nil. You want your pointer to refer to the result of the .first call.

Upvotes: 1

Related Questions