On Jul 28, 10:38=A0pm, "Michael W. Ryder" <_mwry... / worldnet.att.net>
wrote:
> RichardOnRails wrote:
> > On Jul 28, 5:52 pm, RichardOnRails
> > <RichardDummyMailbox58... / USComputerGurus.com> wrote:
> >> On Jul 28, 3:34 pm, "Michael W. Ryder" <_mwry... / worldnet.att.net>
> >> wrote:
>
> >>> RichardOnRails wrote:
> >>>> On Jul 27, 12:36 pm, Prateek Agarwal <prateek.a... / gmail.com> wrote:
> >>>>> I am new to Ruby and am still learning some of the basic stuff.
> >>>>> What's the method name for the Power operation(as in 'a' to the pow=
er
> >>>>> 'b')?
> >>>>> --
> >>>>> Posted viahttp://www.ruby-forum.com/.
> >>>> Whoops. =A0I forgot to paste in the program (sorry), =A0which follow=
s:
> >>>> =3Dbegin =A0 =A0 =A0 =A0# Note this comments out all lines until the=
 =3Dend
> >>>> def power(a,b)
> >>>> result=3Da**b =A0 # "a" should be "a.chomp.to_1"; ditto "b";
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 # the "chomp" removes the newline which =
the user presses
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 # "result" is unnecessary
> >>>> result.to_i =A0 # does nothing
> >>>> return result # unnecessary:
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 # Since we've eliminated everything else=
, the method
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 # has only one statement, i.e. the expre=
ssion
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 # a ** b with the replacements suggested=
 above
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 # Ruby returns the last statement's valu=
e
> >>>> end
> >>>> puts "a=3D" =A0 =A0 # use printf rather than puts (which appends a n=
ewline)
> >>>> a=3Dgets
> >>>> a.to_i =A0 =A0 =A0 =A0# does nothing; "a" does not get change, and t=
he
> >>>> result
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 # is discarded
> >>>> puts "b=3D" =A0 =A0 # same as "a"
> >>>> b=3Dgets
> >>>> b.to_i =A0 =A0 =A0 =A0 =A0 # ditto as for "a"
> >>>> c=3Dpower(a,b) =A0# numeric result assigned to c, probably an intege=
r but
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 # not necessarily
> >>>> puts "c=3D#{c}" # These final two lines might be more elegantly
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 # written in Ruby as suggested below
> >>>> =3Dend
> >>>> # The result of all these changes are the following 8 lines
> >>>> # (plus blank lines); save them, say, as: =A0 =A0 =A0 Test.rb
> >>>> # and run them as: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0ruby Test.rb
> >>>> def power(a,b)
> >>>> =A0 a.chomp.to_i**b.chomp.to_i
> >>>> end
> >>>> printf "a=3D"
> >>>> a =3D gets
> >>>> print "b=3D"
> >>>> b =3D gets
> >>>> puts "%d**%d =3D %d" % [a, b, power(a,b)]
> >>> As an "improvement" to your code I would take the chomp and to_i out =
of
> >>> the power function to make it more generic and add them after the get=
s.
> >> Hi Michael,
>
> >> Your point is well taken.
>
> >> I did that for a newbie to point the stuff that's needs to be done to
> >> get things working as he intends. =A0He's not likely to look up "to_i"
> >> to learn all its machinations.
>
> >> In fact, there's one more that I would have thrown in, had I
> >> remembered it: strip.
>
> >> I do that in string-handlers I write:
> >> 1. in part, to remind myself what to_i would do for me automatically
> >> 2. in part, to guarantee that that stuff gets done even if new
> >> versions of Ruby eliminate some helpful feature.
> >> 3. in part, because I might decide to extend a program using the input
> >> string as though it contained only the digits that to_i revealed,
> >> forgetting that a lot of "baggage" had been removed.
>
> >> Perhaps having taught Computer Technology at AU in DC for a decade
> >> gives me a different perspective than production program with a lean-
> >> and-mean code perspective.
>
> >> Do I make any sense?
>
> >> Best wishes,
> >> Richard
>
> > Hey Michael,
>
> > After posting my reply to you, =A0I re-read you post and realized I
> > misinterpreted it. =A0I favor putting all that baggage in the OP's
> > "power" routine rather than having to remember that stuff when writing
> > each invocation of "power".
>
> > Does that make any sense?
>
> > Best wishes,
> > Richard
>
> The reason I stripped out the conversions from the power function is to
> allow its use with either integers or floats. =A0Plus I prefer to convert
> input to it's desired form as soon as possible, rather than having to
> remember to convert it everywhere it is used. =A0If a is an integer there
> is no need to store it as a string with \n on the end and then having to
> remember to chomp and to_i it everywhere you need it. =A0What if the OP
> wanted to use a and b later such as showing the power of a and b and
> then the power of a+1 and b+1? =A0He would have to again chomp the
> variables and convert them to integers, add one to each, then convert
> them back to strings before feeding them to the power function.

Good points, Michael.

Let's hope the OP learned something from all of this.

Best wishes,
Richard