From: Matthew Kerwin Date: 2013-01-25T13:23:07+09:00 Subject: Re: General Ruby syntax questions --f46d042f95beb32b7704d415487f Content-Type: text/plain; charset=ISO-8859-1 On 25 January 2013 13:16, Scott Price 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#<= < http://www.ruby-doc.org/core-1.9.3/Fixnum.html#method-i-3C-3D> 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#* 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#+ 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 Kerwin, 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 --f46d042f95beb32b7704d415487f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On 25 January 2013 13:16, Scott Price &l= t;lists@ruby-foru= m.com> wrote:
-- Can sombody please break these down and let me know wha= t each line of
these examples are doing. I've used c++ a little, but I'm new to Ru= by.
Any response would be greatly appreciated.
=A0
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 wh= at a C/C++ programmer would write. =A0As such, seeing as you've "u= sed C++ a little," surely you could work out what is going on?

Anyway, I'll break down a function or t= wo 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 'expo= nent'.
Given the context I can't say for sure on wh= ich object this method is defined, but for the sake of tutorial, for now le= t's just say it's a "global" function.

=A0 result =3D 1
Create a variable called 'r= esult', and assign to it the value 1. =A0Under the hood I'd interpr= et that as: make the 'result' variable refer to the singleton Fixnu= m object '1'
=A0
=A0 i =3D 1
Ditto, but called 'i'
<= div style>=A0
=A0 while i <=3D exponent
Invoke the method &= #39;<=3D' on the object referred to by variable 'i', with it= s parameter being the object referred to by variable 'exponent'.

Use= the result (return value) of that method as the condition in a 'while&= #39; expression.
=A0
=A0 =A0 result =3D result * base
Invoke the meth= od '*' on the object referred to by variable 'result', with= its parameter being the object referred to by variable 'base'.

Assign the result of= that method to the 'result' variable. =A0(i.e. update the 'res= ult' variable to point to the object returned from #* )

=A0 =A0 i +=3D 1
Syntactic sugar for: =A0`i =3D = i + 1`

Invoke the method '+' on= the object referred to by variable 'i', with its parameter being t= he single Fixnum object '1'.

Assign the result of that method= to the 'i' variable. =A0(I.e. update the 'i' variable to p= oint to the object returned from #+ )
=A0
=A0 end
Marks the end of the current scope/block= /whatever you want to call it.
In this case implies an exec= ution jump back to the 'while' statement three lines above.
=A0
=A0 result
end
= These two lines go hand-in-hand. =A0The 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'. =A0I.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, a= nd the last expression in this function was the 'result' bit, the r= eturn value of this function is the final value of the 'result' var= iable.

Note: a regular rubyist would probably just write= : =A0`base ** exponent`

Actually, I won'= t write out the others, because I don't have time. =A0Hopefully this ha= s been illuminating in some way. =A0And if I've gotten anything wrong, = someone please correct me.

Cheers
--
=A0 Matthew Kerwin, B= .Sc (CompSci) (Hons)
=A0 http://matthew.kerwin.net.au/
=A0 ABN: 59-013-727-651
=A0 "You'll never find a programming language that frees
=A0 you from the burden of clarifying your ideas." - xkcd
--f46d042f95beb32b7704d415487f--