What is Enumerator object? (Created with String#gsub) What is Enumerator object? (Created with String#gsub) ruby ruby

What is Enumerator object? (Created with String#gsub)


An Enumerator object provides some methods common to enumerations -- next, each, each_with_index, rewind, etc.

You're getting the Enumerator object here because gsub is extremely flexible:

gsub(pattern, replacement) → new_strgsub(pattern, hash) → new_strgsub(pattern) {|match| block } → new_strgsub(pattern) → enumerator 

In the first three cases, the substitution can take place immediately, and return a new string. But, if you don't give a replacement string, a replacement hash, or a block for replacements, you get back the Enumerator object that lets you get to the matched pieces of the string to work with later:

irb(main):022:0> s="one two three four one"=> "one two three four one"irb(main):023:0> enum = s.gsub("one")=> #<Enumerable::Enumerator:0x7f39a4754ab0>irb(main):024:0> enum.each_with_index {|e, i| puts "#{i}: #{e}"}0: one1: one=> " two three four "irb(main):025:0> 


When neither a block nor a second argument is supplied, gsub returns an enumerator. Look here for more info.

To remove it, you need a second parameter.

attributes[-1].gsub("Photo:", "")

Or

attributes[-1].delete("Photo:")

Hope this helps!!