Home > Blockchain >  Is there any way to call all the methods inside the class with the single line code in Ruby?
Is there any way to call all the methods inside the class with the single line code in Ruby?

Time:10-28

I have done online research on this and also searched for the solution on SO but still didn't got any. Need a simple, efficient, time and space saving way to call all the functions in a class

Here i have a Class with many methods defined inside. after the end of the Class, i have to call all the defined methods to execute the block of code inside each methods.

class Sample

    def initialize(arg1, arg2)
        @arg1 = arg1
        @arg2 = arg2
    end

    def method1
        puts @arg1
    end 

    def method2
        puts @arg2
    end 

    def method3
        puts "This is method3"
    end  

    def method4
        puts "This is method4"
    end 

    .............
    .............
    ............. etc...

end

Now creating an object for calling the class and method

object = Sample.new(par1, par2)
object.method1
object.method2
object.method3
object.method4
.............
............. etc...

calling the methods one by one using the object.method_name(parameter) is really hard and taking very long space and time. is it possible to call all the methods by a single line code (or) with any other efficient way?

CodePudding user response:

is it possible to call all the methods by a single line code

Yes, that is possible.

Personally, I don't get the obsession with squeezing everything into a single line. It does not make code smaller, it does not make code better, it does not make code easier to read. But it is technically possible.

In Ruby, line breaks are always optional. They can always be replaced with something else. If the line break is used as an expression separator, it can be replaced with a semicolon (;), which is also an expression separator. If the line break is used to terminate some syntactic element, it can be replaced with either a semicolon (;), a keyword (for example then in if and unless, when in case, do in for, while, and until, and so on), and sometimes just whitespace.

So, you could write your code in a single line like this:

object = Sample.new(par1, par2); object.method1; object.method2; object.method3; object.method4; # … etc …

calling the methods one by one using the object.method_name(parameter) is really hard and taking very long space and time.

Whether you write the code on one line or multiple lines has no effect on the space or time requirements of your program.

If you execute the methods sequentially, the space requirement will be the maximum of the space requirements of all the methods and the time requirement will be the sum of the time requirements of all the methods.

You can execute the methods in parallel. In that case, the space requirement will be the sum of the space requirements of all the methods and the time requirement will be the maximum of the time requirements of all the methods plus any time needed to coordinate the parallel execution and merge the results back together. Also, executing the methods in parallel will change the result of the program!

Either way, you can only improve either space or time, not both.

CodePudding user response:

You need to add any prefix like auto __call__ in method name that you need to call automatically in single line dynamic code

just find method names using simple string operation in method names array then call them using send method

class Sample
    def initialize(arg1, arg2)
        @arg1 = arg1
        @arg2 = arg2
    end

    def auto__call__method1
        puts @arg1
    end 

    def auto__call__method2
        puts @arg2
    end 

    def auto__call__method3
        puts "This is method3"
    end  

    def auto__call__method4
        puts "This is method4"
    end 
end

object = Sample.new('arg1', 'arg2')
object.public_methods
      .select{ |m| m.to_s.include? 'auto__call__'}
      .each{ |auto__call_method| object.send(auto__call_method) }

output

arg2
This is method3
This is method4
arg1

if you need to any presidency in method calling then add presidency prefix then sort method names then call it

like below

class Sample

    def initialize(arg1, arg2)
        @arg1 = arg1
        @arg2 = arg2
    end

    def auto__call_4_method1
        puts @arg1
    end 

    def auto__call_3_method2
        puts @arg2
    end 

    def auto__call_2_method3
        puts "This is method3"
    end  

    def auto__call_1_method4
        puts "This is method4"
    end 
end
object = Sample.new('arg1', 'arg2')
object.public_methods
      .select{ |m| m.to_s.include? 'auto__call_'}
      .sort
      .each{ |my_method_name| object.send(my_method_name) }

output

This is method4
This is method3
arg2
arg1
  • Related