Forum: Ruby Passing method to method

Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2017-01-02 10:46
class HighLowGame

      attr_accessor :randno, :guess



      def generateRandomNumber
            randno = rand(99)
            return randno
      end

      def getUserInput
              puts "Enter your guess"
              guess = gets.chomp.to_i

              return guess
      end

      def getResult(getUserInput())
            if guess < randno
               puts "#{guess } is lower than Magic Number"
            elsif guess > randno
               puts "#{guess} is higher than Magic Number"

            else  guess == randno
               puts "You are right!"
            end
      end


end

        suresh = HighLowGame.new
        suresh.getUserInput
        suresh.getResult(getUserInput())

---------------------------------------------------------

Is it possible to pass method to methods?
I tried googling most of it is passing block to methods.
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2017-01-03 20:50
Suresh Ilankovan wrote in post #1185415:

> Is it possible to pass method to methods?
> I tried googling most of it is passing block to methods.

A) a method can be call by his name :

irb>class A
  def b() p "b" end
  def c(x,y) p "c #{x} #{y}" end
end

irb>A.new.send("b")
"b"
=> "b"
irb> A.new.send("c",1,2)
"c 1 2"
=> "c 1 2"


B) you can get method's object and use it :

irb> x=A.new.method("b")
=> #<Method: A#b>
irb> x.call()
"b"
=> "b"
irb>
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-01-05 09:25
Regis d'Aubarede wrote in post #1185420:
> irb>A.new.send("b")

For safety, I would recommend to use public_send instead of send, unless
you know what you are doing. When using send, you would also execute
private methods without getting any warning.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.