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.
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