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 power >> 'b')? >> -- >> Posted viahttp://www.ruby-forum.com/. > > Whoops. I forgot to paste in the program (sorry), which follows: > > =begin # Note this comments out all lines until the =end > def power(a,b) > result=a**b # "a" should be "a.chomp.to_1"; ditto "b"; > # the "chomp" removes the newline which the user presses > # "result" is unnecessary > result.to_i # does nothing > return result # unnecessary: > # Since we've eliminated everything else, the method > # has only one statement, i.e. the expression > # a ** b with the replacements suggested above > # Ruby returns the last statement's value > end > > puts "a=" # use printf rather than puts (which appends a newline) > a=gets > a.to_i # does nothing; "a" does not get change, and the > result > # is discarded > puts "b=" # same as "a" > b=gets > b.to_i # ditto as for "a" > c=power(a,b) # numeric result assigned to c, probably an integer but > # not necessarily > puts "c=#{c}" # These final two lines might be more elegantly > # written in Ruby as suggested below > =end > > # The result of all these changes are the following 8 lines > # (plus blank lines); save them, say, as: Test.rb > # and run them as: ruby Test.rb > > def power(a,b) > a.chomp.to_i**b.chomp.to_i > end > > printf "a=" > a = gets > print "b=" > b = gets > puts "%d**%d = %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 gets.