On 25 January 2013 13:16, Scott P. [email protected] wrote:
– Can sombody please break these down and let me know what each line of
these examples are doing. I’ve used c++ a little, but I’m new to Ruby.
Any response would be greatly appreciated.
First, a question: where did you find this code? It’s not what we could
consider “typical Ruby,” in fact it looks a lot like what a C/C++
programmer would write. As such, seeing as you’ve “used C++ a little,”
surely you could work out what is going on?
Anyway, I’ll break down a function or two the way I understand the
interpreter to work (note: not necessarily how any of them actually
work)
in case it’s helpful.
def pow(base, exponent)
Define a method called ‘pow’.
It has two named parameters, called ‘base’ and ‘exponent’.
Given the context I can’t say for sure on which object this method is
defined, but for the sake of tutorial, for now let’s just say it’s a
“global” function.
result = 1
Create a variable called ‘result’, and assign to it the value 1. Under
the
hood I’d interpret that as: make the ‘result’ variable refer to the
singleton Fixnum object ‘1’
i = 1
Ditto, but called ‘i’
while i <= exponent
Invoke the method ‘<=’ on the object referred to by variable ‘i’, with
its
parameter being the object referred to by variable ‘exponent’.
See: Fixnum#<= <
Class: Fixnum (Ruby 1.9.3)>
Use the result (return value) of that method as the condition in a
‘while’
expression.
result = result * base
Invoke the method '’ on the object referred to by variable ‘result’,
with
its parameter being the object referred to by variable ‘base’.
See: Fixnum#
<http://www.ruby-doc.org/core-1.9.3/Fixnum.html#method-i-2A
Assign the result of that method to the ‘result’ variable. (i.e. update
the ‘result’ variable to point to the object returned from #* )
i += 1
Syntactic sugar for: i = i + 1
Invoke the method ‘+’ on the object referred to by variable ‘i’, with
its
parameter being the single Fixnum object ‘1’.
See: Fixnum#+
<Class: Fixnum (Ruby 1.9.3)
Assign the result of that method to the ‘i’ variable. (I.e. update the
‘i’
variable to point to the object returned from #+ )
end
Marks the end of the current scope/block/whatever you want to call it.
In this case implies an execution jump back to the ‘while’ statement
three
lines above.
result
end
These two lines go hand-in-hand. The former is a simple statement which
evaluates to the value of the ‘result’ variable.
‘end’ again marks the end of the current scope/block/etc., in this case
the
chunk of code that started with ‘def’. I.e. it indicates the end of the
function.
Since the value of a chunk of code is always the value of the last
expression evaluated in it, and the last expression in this function was
the ‘result’ bit, the return value of this function is the final value
of
the ‘result’ variable.
Note: a regular rubyist would probably just write: base ** exponent
Actually, I won’t write out the others, because I don’t have time.
Hopefully this has been illuminating in some way. And if I’ve gotten
anything wrong, someone please correct me.
Cheers
Matthew K., B.Sc (CompSci) (Hons)
http://matthew.kerwin.net.au/
ABN: 59-013-727-651
“You’ll never find a programming language that frees
you from the burden of clarifying your ideas.” - xkcd