From: Walton Hoops Date: 2009-09-11T01:50:46+09:00 Subject: Re: Tutorial challenge program help Chris Wrote: > > def monkey?( var ) > > var != Math::PI && var != "BYE" > > end > So let me get this one straight. Doing it like this would mean that > instead of calling monkey? on the variable i would call the variable on > the monkey? method.? I'm sorry, I did not truly understand the > difference in there would it not be the exact same thing? Not really. In this example monkey? is a function which takes a single argument, var. It checks to see if this argument (var) is equal to pi. In your version, the function monkey? is being added to the String class itself, which then checks whether the string object it is called from is equal to pi (which it can never be as pi is a number and the string must be a string). > > later, seeing :uninitialized in all places where you currently have > > Math::PI > > will make a lot more sense. > > Hmm. So if i use :uninitialised then it doesnt really mean anything, > its > just like a, placeholder? if I were to input :uninitialized in the > prompt would it still give the .monkey? call though? Has the tutorial you're following showed you irb yet? It comes with Ruby, and it's great for seeing how things work. It lets you type Ruby statements for immediate execution. A quick demonstration of what he was talking about with symbols: irb(main):001:0> def monkey? (var) irb(main):002:1> var==:noinit irb(main):003:1> end => nil ##So here I define my function, which simply checks to see if it's ##argument is equal to the :noinit symbol ##the => nil is what was produced by defining this function (def ##never returns anything) irb(main):004:0> monkey? :noinit => true ##Call monkey? On the :noinit symbol itself, sure enough :noinit=:noinit irb(main):005:0> monkey? ':noinit' => false ##Call monkey? On the string ':noinit'. Notice they are not equal irb(main):006:0> reply=gets.chomp :noinit => ":noinit" irb(main):007:0> monkey? reply => false ##input ":noinit" and try calling monkey? on that. Again not equal. Symbols are nice tools. When you use a symbol like :noinit, it will only ever equal the symbol :noinit, nothing else. This all however, is no doubt jumping the gun, and getting well ahead of your tutorial. I think your best bet would be to move on, and revisit this example later to see how you can improve upon it.