NoMethodError split for array using optional parameter

Error:
script.rb:23:in getCookies': undefined methodsplit’ for [“comeon”,
“blah”]:A
rray (NoMethodError)

My method using an optional argument:
def getCookies(c)
c[1]=“blah”
$cookiesA = [] if $cookiesA == nil
$cookiesA = c.split(";") unless c == nil
$cookiesH = {}
$cookiesA.each {|e|
e.match(/(.
)=(.*)/)
$cookiesH[$1]=$2
}
$cookiesA = $cookiesH.map{|k, v| “#{k}=#{v}”}
$cookies = $cookiesA.join(";")
return $cookies
end

I don’t know how to fix this. I wanted to make a Cookies class…
So I can’t use a lot of methods on optional arguments?
So… I’m probably doing this wrong, and that’s probably not even a splat
operator.

Now I’m using *args:

def cookies(args) #If a value is passed, it is added to cookies, else
cookies are returned.
$cookiesH = {} if $cookiesH == nil
#adds cookies to cookies hash
if (args[0] != nil) then
if (args[0].is_a? Array) then c = args[0].join(";") else c =
args[0].to_s end
puts c
c.split(";").each {|e|
e.match(/(.
)=(.*)/)
$cookiesH[$1]=$2 unless $2 == nil
}
end

#reports cookies in string format
if (args[0] == nil) then
cookies = $cookiesH.map{|k, v| “#{k}=#{v}”}.join(";")
return cookies
end
end

You’re trying to call split on an Array. I think you intended to do it
on a String.

Hahahahaha. Thank you.
Realized that a few seconds before seeing your post. Amazing what a long
break can do…

Thanks robert. I’ll spend some time thinking about what you wrote. When
I learn about OOP, perhaps that will help, too.
So you know, this is the method I ‘ended up’ with:

def cookies(args) #If a value is passed, it is added to cookies, else
cookies are returned.
$cookiesH = {} if $cookiesH == nil
#adds cookies to cookies hash
if (args[0] != nil) then
if (args[0].is_a? Array) then c = args[0].join(";") else c =
args[0].to_s end
c.split(";").each {|e|
e.match(/(.
)=(.*)/)
key = $1.strip unless $1 == nil
k = key.downcase unless $1 == nil
if ($2 != nil and $1 != nil and k != “expires” and k != “max-age”
and k != “domain” and k != “path”) then
$cookiesH[key]=$2
end
}
end

#reports cookies in string format
if (args[0] == nil) then
cookies = $cookiesH.map{|k, v| “#{k}=#{v}”}.join("; ")
return cookies
end
end

I’m not entirely sure what you’re doing there, but I’ve tried to tidy up
the logic a bit for you:

def cookies( arg = nil )
@cookies_hash ||= {}
return cookies_to_s if arg.nil?
arg = arg.split(’;’) unless arg.is_a?( Array )

arg.each { |e|
res = e.scan(/([^=])=([^=])/).flatten
res[0].downcase!
next if res.size != 2 || %w(expires max-age domain
path).include?(res[0])
@cookies_hash[ res[0] ] = res[ 1 ]
}
cookies_to_s
end

def cookies_to_s
@cookies_hash.map{|k, v| “#{k}=#{v}”}.join("; ")
end

On Sun, May 5, 2013 at 7:58 PM, Sarek M. [email protected]
wrote:

$cookiesH = {}
So I can’t use a lot of methods on optional arguments?

There are so many things in need of improvement with your code that I
hardly know where to start. First of all there is no class at all.
Then,
if you code a class you don’t access global variables like you do here
but
instance variables. Global variables are considered harmful for various
reasons (see [1] and [2]).

Then, conventionally we use get_cookies() as method name in Ruby and
CamelCase only for class and module names. Your error stems from the
fact
that you invoke Array#split but you probably rather want String#split.
Your local variable c is never nil because of the splat operator in
the
argument list so the test is superfluous. Ah, and array indices start
with
0 and not 1 in Ruby.

Kind regards

robert

[1] Considered harmful - Wikipedia
[2] http://c2.com/cgi/wiki?GlobalVariablesConsideredHarmful