• 都是把env包装一下,但ActionDispatch::Request会方便一点不用写HTTP_

    # Converts an HTTP header name to an environment variable name if it is
    # not contained within the headers hash.
    def env_name(key)
      key = key.to_s
      if key =~ HTTP_HEADER
        key = key.upcase.tr("-", "_")
        key = "HTTP_" + key unless CGI_VARIABLES.include?(key)
      end
      key
    end
    
    ENV_METHODS.each do |env|
      class_eval <<-METHOD, __FILE__, __LINE__ + 1
        def #{env.sub(/^HTTP_/n, '').downcase}  # def accept_charset
          get_header "#{env}".freeze            #   get_header "HTTP_ACCEPT_CHARSET".freeze
        end                                     # end
      METHOD
    end
    

    此外rails还会mixin其它一些便利东西,所以需要一个新的类来代表request

  • 在其它文本编辑器写好了再贴过去……

  • 跟rails的route差不多,试着撸一个简陋的:

    module Search
    
       def self.ing &blk
         Root.new.tap do |r|
           r.instance_eval(&blk)
           puts r
         end
       end
    
      def children
        @chidlren ||= []
      end
    
      def with condition, value
        children << With.new(condition, value)
      end
    
      def without condition, value
        children << Without.new(condition, value)
      end
    
      def any_of &blk
        a = Any.new
        a.instance_eval &blk
        children << a
      end
    
      def all_of &blk
        a = All.new
        a.instance_eval &blk
        children << a
      end
    
      def children_to_s
        children.map &:to_s
      end
    
      class Root
        include Search
        def to_s
          children.first.to_s
        end
      end
    
      class Any
        include Search
        def to_s
          '(' + children_to_s.join(' or ') + ')'
        end
      end
    
      class All
        include Search
        def to_s
          '(' + children_to_s.join(' and ') + ')'
        end
      end
    
      class With
        def initialize cond, v
          @cond, @v = cond, v
        end
    
        def to_s
          "#{@cond} = #{@v}"
        end
      end
    
      class Without
        def initialize cond, v
          @cond, @v = cond, v
        end
    
        def to_s
          "#{@cond} != #{@v}"
        end
      end
    end
    
    Search.ing do
      any_of do
        with :col1, 1
        with :col2, 2
        all_of do
          with :col3, 3
          without :col4, 4
        end
      end
    end
    
    # output
    # (col1 = 1 or col2 = 2 or (col3 = 3 and col4 != 4))
    
  • namespace冲突啊,都是module Bootstrap

    看readme开头:

    Bootstrap 4 ruby gem for Ruby on Rails (Sprockets) and Compass. For Sass versions of Bootstrap 3 and 2 see bootstrap-sass instead.

    其实看你要哪个版本的bootstrap就好了,不需要两个都加

    1. rails db:rollback会调用down,或者change中的反义操作(如果有对应的反义操作)

    2. 若up中add_column,down中remove_column,那么调完up再调down(migrate然后rollback),结果就相当于没加过column

    感觉你没跟着做一遍

  • 健壮参数的写法 at 2017年08月06日

    active_support有给Hash补上一个叫except的方法

    但是你无法预估前端会传来什么破坏性的参数,所以反选是不健壮的

  • Ruby 重命名文件权限报错 at 2017年08月03日

    1、改文件名须有该文件所在目录的w权限

    2、从你报错信息看,连.和..都读出来了

    3、理论上filename,newfile需带上目录名"生命"

  • LZ似乎没搞懂运行时self的指向、类与实例的区别、继承链,这三个问题

  • 漫谈 Kernel at 2017年06月26日

    用raise来调试?

  • 联表查询写法的疑问 at 2017年06月23日
    top10 = Post.group(:channel_id).order('count(*) desc').limit(10).count
    Channel.find top10.keys