开发者

Can I write instance or class method inside if condition?

开发者 https://www.devze.com 2023-03-17 15:49 出处:网络
Can I write something like this inside model ? if true def instance_method end def class_method 开发者_StackOverflow社区 end

Can I write something like this inside model ?

if true
  def instance_method
  end

  def class_method
 开发者_StackOverflow社区 end
end


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消