From: Robert Klemme Date: 2013-05-06T15:57:10+09:00 Subject: Re: NoMethodError split for array using splat operator --f46d044789038c849704dc0735d6 Content-Type: text/plain; charset=ISO-8859-1 On Sun, May 5, 2013 at 7:58 PM, Sarek Mather wrote: > Error: > script.rb:23:in `getCookies': undefined method `split' 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? > 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] http://en.wikipedia.org/wiki/Considered_harmful [2] http://c2.com/cgi/wiki?GlobalVariablesConsideredHarmful -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --f46d044789038c849704dc0735d6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Sun, May 5, 2013 at 7:58 PM, Sarek Mather <= lists@ruby-forum.= com> wrote:
Error:
script.rb:23:in `getCookies': undefined method `split' for ["c= omeon",
"blah"]:A
rray (NoMethodError)

My method using an optional argument:
def getCookies(*c)
=A0 =A0 c[1]=3D"blah"
=A0 $cookiesA =3D [] if $cookiesA =3D=3D nil
=A0 =A0 $cookiesA =3D c.split(";") unless c =3D=3D nil
=A0 $cookiesH =3D {}
=A0 $cookiesA.each {|e|
=A0 =A0 e.match(/(.*)=3D(.*)/)
=A0 =A0 $cookiesH[$1]=3D$2
=A0 =A0 }
=A0 $cookiesA =A0=3D $cookiesH.map{|k, v| "#{k}=3D#{v}"}
=A0 $cookies =3D $cookiesA.join(";")
=A0 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?
=

There are so many things in need of improvement with your code that I hard= ly know where to start. =A0First of all there is no class at all. =A0Then, = if you code a class you don't access global variables like you do here = but instance variables. =A0Global variables are considered harmful for vari= ous reasons (see [1] and [2]).

Then, conventionally we use get_cookies() as method name in Ruby and Camel= Case only for class and module names. =A0Your error stems from the fact tha= t you invoke Array#split but you probably rather want String#split. =A0Your= local variable c is *never* nil because of the splat operator in the argum= ent list so the test is superfluous. =A0Ah, and array indices start with 0 = and not 1 in Ruby.

Kind regards

robert



--
remember.guy do |as, often| as.you_can - without end<= br>http://blog.rubybestpract= ices.com/
--f46d044789038c849704dc0735d6--