Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Properly handle setting attributes to an array on Ruby 1.9

  • Loading branch information...
commit 7cba30f076590116f5b825f0fca4319019d15cae 1 parent 7a16ead
@benlangfeld authored
View
7 lib/niceogiri/core_ext/nokogiri.rb
@@ -16,7 +16,12 @@ class Node
# @param [#to_s, nil] value the new value or nil to remove it
def []=(name, value)
name = name.to_s
- value.nil? ? remove_attribute(name) : attr_set(name, value.to_s)
+ if value.nil?
+ remove_attribute name
+ else
+ value = value.is_a?(Array) ? value.join : value
+ attr_set name, value.to_s
+ end
end
alias_method :nokogiri_xpath, :xpath
View
9 spec/niceogiri/core_ext/nokogiri_spec.rb
@@ -30,6 +30,15 @@
attrs[:foo] = 1
attrs[:foo].must_equal '1'
+ attrs[:some_attr] = :bah
+ attrs[:some_attr].must_equal 'bah'
+ end
+
+ it 'joins an array into a string when passed to the attribute setter' do
+ attrs = Nokogiri::XML::Node.new('foo', @doc)
+ attrs[:foo] = 1
+ attrs[:foo].must_equal '1'
+
attrs[:some_attr] = [:bah, :boo]
attrs[:some_attr].must_equal 'bahboo'
end
Please sign in to comment.
Something went wrong with that request. Please try again.