【问题标题】:Is there any way to call all the methods inside the class with the single line code in Ruby?有没有办法用Ruby中的单行代码调用类中的所有方法?
【发布时间】:2022-11-07 21:25:05
【问题描述】:

我已经对此进行了在线研究,并在 SO 上搜索了解决方案,但仍然没有。 需要一种简单、高效、节省时间和空间的方式来调用class 中的所有函数

这里我有一个class,里面定义了许多methods。在class 结束后,我必须调用所有已定义的methods 来执行每个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

现在创建一个object 来调用classmethod

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

使用object.method_name(parameter) 一个一个地调用methods 真的很难,而且需要很长的空间和时间。 是否可以通过单行代码(或)以任何其他有效方式调用所有methods

【问题讨论】:

  • 调用一个类中的所有函数: 你的意思是自动地查找(并调用)类中定义的所有方法(当然除了从父类继承的方法)?或者自动查找满足某个命名模式的类的所有方法?
  • 您的意思是自动查找(并调用)类中定义的所有方法=> 是的,我期望会有一些“直接方法”,如“.sort、.call 等......可以与类名一起添加,如 #=> { class_name.some_method } 将使所有要自动调用的方法:)
  • 您可以动态查找类的所有方法。查看public_methodsinstance_methods 的Ruby 文档。

标签: ruby class methods


【解决方案1】:

是否可以通过单行代码调用所有methods

是的,这是可能的。

就个人而言,我不会痴迷于将所有内容都压缩成一行。它不会使代码更小,不会使代码更好,也不会使代码更易于阅读。但这在技术上是可行的。

在 Ruby 中,换行符是总是可选的。它们总是可以用其他东西代替。如果换行符用作表达式分隔符,可以用分号(;)代替,分号也是表达式分隔符。如果换行符用于终止某些语法元素,则可以将其替换为分号 (;)、关键字(例如 if 中的 thenunlesswhen 中的 case , do in for, while, 和 until 等等),有时只是空格。

因此,您可以像这样在一行中编写代码:

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

使用object.method_name(parameter) 一个一个地调用methods 真的很难,而且需要很长时间的空间和时间。

无论您是在一行还是多行上编写代码,都不会影响程序的空间或时间要求。

如果按顺序执行方法,则空间需求将是所有方法的空间需求中的最大值,而时间需求将是所有方法的时间需求之和。

您可以并行执行这些方法。在这种情况下,空间需求将是所有方法的空间要求和时间要求将是最大所有方法的时间要求加上协调并行执行并将结果重新合并在一起所需的任何时间。此外,并行执行这些方法会改变程序的结果!

无论哪种方式,你只能提高任何一个空间或者时间,不是两者兼而有之。

【讨论】:

    【解决方案2】:

    您需要在需要在单行动态代码中自动调用的方法名称中添加任何前缀,例如 auto __call__

    只需在方法名称数组中使用简单的字符串操作查找方法名称,然后使用send 方法调用它们

    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) }
    

    输出

    arg2
    This is method3
    This is method4
    arg1
    

    如果您在方法调用中需要任何总统职位然后添加总统职位前缀然后对方法名称进行排序然后调用它

    像下面

    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) }
    

    输出

    This is method4
    This is method3
    arg2
    arg1
    

    【讨论】:

    • 或许值得注意的是,public_methods 也返回父类的方法。
    • 但是,如果您将其调用为public_methods(false),则继承的将被忽略。也许这对你的例子会更好。
    • 是的,你是对的
    【解决方案3】:

    您能否提供更多上下文这些方法应该做什么? 我想我会在那里添加一个额外的方法来为你调用它们。

    def call_em_all
      method1
      method2
      method3
      method4
    end
    

    【讨论】:

      猜你喜欢
      • 2010-09-06
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2013-09-20
      相关资源
      最近更新 更多