Can we define utility class with instance methods and then in another class, using object of the utility class, call the instance methods?
For example,
class Usertype # utility class
  def add(a, b)
    c = a + b
    return c
  end
end
class User
  user = Usertype.new
  def test
    return user.add(1,2)
  end
end
Can this be done?
I am not sure what you are asking but here is what you can do:
class Usertype
   def add(a,b)
     return a + b
   end
end
class User
  def test
    u = Usertype.new
    return u.add(1,2)
  end
end
Or you can use an instance variable in user:
class User
   def initialize
    @u = Usertype.new
  end
  def test
    return @u.add(1,2)
  end
end
                        Yup, you can achieve that by using modules:
module Usertype
  def self.add(a,b)
    a + b
  end
end
class User
  def test
    Usertype.add 1, 2
  end
end
u = User.new
u.test
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With