开发者

Can I write instance or class method inside if condition?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜