tags:

views:

39

answers:

2

I'd like to split and do a substitution in a string in one chained command. Here's my example including the error message:

>> filebase
=> "Ueki_-_Hello_World"
>> filebase.split("_-_").gsub("_"," ")
NoMethodError: private method `gsub' called for ["Ueki", "Hello_World"]:Array
    from (irb):16

It works when I save the result of "split" in a temporary variable. Do I really need that?

+4  A: 

String#split returns an Array. Arrays don't have a gsub method.

It's not clear what it is you are trying you achieve. Is this what you are looking for?

filebase.split("_-_").map {|s| s.gsub("_"," ") }
Jörg W Mittag
Oh, yes, you are right. Thanks very much!
Bernd Plontsch
To be technical, it doesn't have a public `gsub` method, just a private one for keyword-like use.
Andrew Grimm
A: 

You can use either map or collect:

filebase.split("_-_").map {|s| s.gsub("_"," ") }
filebase.split("_-_").collect {|s| s.gsub("_"," ") }
juandebravo