我可以在if条件下写实例或类方法吗?

时间:2022-11-25 07:50:47

Can I write something like this inside model ?

我可以写这样的内部模型吗?

if true
  def instance_method
  end

  def class_method
  end
end

3 个解决方案

#1


2  

As far as I know if statements do not introduce scope in Ruby 1.9.2 so you can use statements like that

就我所知,如果语句没有在Ruby 1.9.2中引入范围,那么您可以使用这样的语句

#2


0  

Is this what you desire?

这是你想要的吗?

if true
    my_object.instance_eval do
        def my_instance_method

        end
    end

    my_object.class_eval do
        def my_class_eval

        end
    end
end

#3


0  

you can use also:

您还可以使用:

   if true
    class << self
      def first_method
      end

      def second_method
      end
    end
   end

but it would be better when you will post some real code here

但是如果你在这里发布一些真正的代码就更好了

#1


2  

As far as I know if statements do not introduce scope in Ruby 1.9.2 so you can use statements like that

就我所知,如果语句没有在Ruby 1.9.2中引入范围,那么您可以使用这样的语句

#2


0  

Is this what you desire?

这是你想要的吗?

if true
    my_object.instance_eval do
        def my_instance_method

        end
    end

    my_object.class_eval do
        def my_class_eval

        end
    end
end

#3


0  

you can use also:

您还可以使用:

   if true
    class << self
      def first_method
      end

      def second_method
      end
    end
   end

but it would be better when you will post some real code here

但是如果你在这里发布一些真正的代码就更好了