Forum: Ruby Passing method to method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
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.
This topic is locked and can not be replied to.