• Iconv Code Review at 2011年11月28日

    我觉得大家都在误用 force encoding 呀 force encoding 只改变字符的编码标记,并不真正改变字符的编码 encode 才改变编码,但是必须知道本来是什么编码

  • Iconv Code Review at 2011年11月28日
    require 'rchardet19'
    
    def encode(string)
      cd = CharDet.detect(string)
      if cd.confidence > 0.6
        string.force_encoding(cd.encoding)
      end
      string.encode("utf-8", :undef => :replace, :replace => "?", :invalid => :replace)
    end
    
  • https://github.com/rails/rails/issues/2963 我也觉得这个错误提示不是很友好...

  • #16 楼 @nowazhu 我也觉得这个测试不靠谱,,因为他的测试数据是随机产生的,每个 store 的 miss 的数量不相同..

  • 咦 好像我搞错了,囧 rz

  • #2 楼 @lgn21st 在继承的时候是有区别的 https://github.com/rails/rails/blob/master/activerecord/lib/active_record/callbacks.rb

    # == Inheritable callback queues
    #
    # Besides the overwritable callback methods, it's also possible to register callbacks through the
    # use of the callback macros. Their main advantage is that the macros add behavior into a callback
    # queue that is kept intact down through an inheritance hierarchy.
    #
    #   class Topic < ActiveRecord::Base
    #     before_destroy :destroy_author
    #   end
    #
    #   class Reply < Topic
    #     before_destroy :destroy_readers
    #   end
    #
    # Now, when <tt>Topic#destroy</tt> is run only +destroy_author+ is called. When <tt>Reply#destroy</tt> is
    # run, both +destroy_author+ and +destroy_readers+ are called. Contrast this to the following situation
    # where the +before_destroy+ method is overridden:
    #
    #   class Topic < ActiveRecord::Base
    #     def before_destroy() destroy_author end
    #   end
    #
    #   class Reply < Topic
    #     def before_destroy() destroy_readers end
    #   end
    #
    # In that case, <tt>Reply#destroy</tt> would only run +destroy_readers+ and _not_ +destroy_author+.
    # So, use the callback macros when you want to ensure that a certain callback is called for the entire
    # hierarchy, and use the regular overwriteable methods when you want to leave it up to each descendant
    # to decide whether they want to call +super+ and trigger the inherited callbacks.
    #
    # *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the
    # callbacks before specifying the associations. Otherwise, you might trigger the loading of a
    # child before the parent has registered the callbacks and they won't be inherited.
    
  • 两种写法不等价吧

  • 这种牛人是怎样炼成的? at 2011年11月27日

    #2 楼 @fredwu 我崇拜 Aaron Patterson, Ilya Grigorik 和 Loren Segal

  • heroku db:pull
    

    可以把 Heroku 的数据库导入到本地开发环境

    ---edit--- db:pull 和 db:push 就是 lgn21st 说的 tab gem 提供的.. ps:回帖没有删除功能?

  • 者也的代码还维护吗 at 2011年11月27日

    @huacnlee 如果 patch 版本不能保证 API 兼容的话还是不要用他的 gem 了... 我找了几个实际的项目,看看他们的 Gemfile: https://github.com/intridea/omniauth/blob/master/omniauth.gemspec https://github.com/plataformatec/devise/blob/master/Gemfile https://github.com/rails/rails/blob/master/Gemfile

    Bundler 的 FAQ:http://gembundler.com/rationale.html

    FAQ: Why Can't I Just Specify Only = Dependencies?
    
    Q: I understand the value of locking my gems down to specific versions, but why can't I just specify = versions for all my dependencies in the Gemfile and forget about the Gemfile.lock?
    
    A: Many of your gems will have their own dependencies, and they are unlikely to specify = dependencies. Moreover, it is probably unwise for gems to lock down all of *their* dependencies so strictly. The Gemfile.lock allows you to specify the versions of the dependencies that your application needs in the Gemfile, while remembering all of the exact versions of third-party code that your application used when it last worked correctly.
    
    By specifying looser dependencies in your Gemfile (such as nokogiri ~> 1.4.2), you gain the ability to run bundle update nokogiri, and let bundler handle updating **only** nokogiri and its dependencies to the latest version that still satisfied the ~> 1.4.2 version requirement. This also allows you to say "I want to use the current version of nokogiri" (gem "nokogiri" in your Gemfile) without having to look up the exact version number, while still getting the benefits of ensuring that your application always runs with exactly the same versions of all third-party code.
    
  • 者也的代码还维护吗 at 2011年11月27日

    我的意思是不指定版本也同步(当然如果你的 Gemfile.lock 如果没有 check 到版本库,那是你 bundle 使用的问题..) cache 到 vendor 与否是另外一个话题了

  • 者也的代码还维护吗 at 2011年11月27日

    @lgn21st 貌似大家没有理解我的意思... 其实我的做法已经是很保守的升级方案了

    Consider a version format of X.Y.Z (Major.Minor.Patch). Bug fixes not affecting the API increment the patch version, backwards compatible API additions/changes increment the minor version, and backwards incompatible API changes increment the major version.
    

    自动升级 bug fix 版本是百利而无一害的.. http://semver.org/ @wxianfeng 服务和本地 gem 是否一样和 cache 到 vendor 下与否一点关系也没有 真正有关系的是 Gemfile.lock 这个文件 Gemfile.lock 是维护开发机和服务器第三方库一致的保证 至于你说出现问题可能是你的 bundle 的使用方法错误

  • Rails 怎么排好 HTML 代码 at 2011年11月27日

    其实我想用 rich text editor。。不要鄙视我:-)

  • 者也的代码还维护吗 at 2011年11月27日

    @wxuabfebg 我上面已经说了用 gem 'devise', "~>1.2.x"这种方式。而不是直接锁定版本号。正如你说的不会每一个 gem 有更新了就去升级。但是却可以一行命令:

    bundle update
    

    来把所有的 bug fix 版本升级到最新。不需要你去改 Gemfile 特别是 rc 版本更不应该锁定

  • 者也的代码还维护吗 at 2011年11月27日

    @wxianfeng 一个正常的项目是应该随着各种插件的升级而升级的,如果没有升级的勇气那么就等着项目以后慢慢烂掉吧

  • 上学的时候学校图书馆里面有搞头书和 The Ruby Way

  • Rails 怎么排好 HTML 代码 at 2011年11月25日

    比较喜欢 ERB,模板语言应该更 HTML,而不是更 Ruby 的嘛..:-)

  • @Rei 这个链接里面的测试结果 mongo-store 速度比其他的快..

  • 者也的代码还维护吗 at 2011年11月25日

    这么锁定没有好处呀 可以锁定小版本号嘛,使用

    gem 'devise', '~>1.2.rc2'
    

    gem 'devise', '~>1.2'
    
  • @aNdReW_Qx memcached 当掉是否崩溃这和你的应用设计的好坏有关,如果你严重依赖缓存容器,无论用什么挂掉都会影响你的应用。 理想的做法是缓存一份,db 里一份,缓存定期或按阀值 write back 到 db。这样即使缓存容器挂掉你的应用也是正常的。

    比如未读数这个例子:

    def unread_num
      unread_num_from_cache || read_attribute(:unread_num)
    end
    
  • 帖子动态推送方案探讨 at 2011年11月25日

    如果插入的时候排好序,用普通数组二分查找也很快。

  • @nowazhu 是被覆盖掉

    when new data is added to the server, and the memory limit was already reached, the server will remove some old data deleting a volatile key, that is, a key with an EXPIRE (a timeout) set, even if the key is still far from expiring automatically
    

    http://antirez.com/post/redis-as-LRU-cache.html

  • flush 是用来干啥的?貌似没有用啊

  • 貌似 redis 可以 expire 还可以 LRU

  • 我也没看过源码。。我只是推测。。 你还是自己看一下吧:-)

  • 局部变量不存在的时候不会。 从你例子的情况看,是因为 db 属性字段都是通过 method missing 实现的,其实 task_ids=这个方法还没定义。

  • 是的,局部变量优先于方法查找...

  • 就是用\r实现的,做爬虫的时候写过一个带进度条的 each:

    class Array
      def each_with_process
        start = Time.now
        each_with_index do |item, index|
          yield(item)
          count = index + 1
          avg = (Time.now - start) * 1.0 / (index + 1)
          eta = ((size - count) * avg) / 60
    
          printf "\r%.2f %%, %d / %d, AVG: %f sec, ETA: %.0f min, PASSED: %d min ... ", count * 100.0 / size, count, size, avg, eta, (Time.now - start) / 60
    
        end
      end
    end
    
  • @lgn21t 我以前写过类似 reddit 和 hack news 的 ranking 算法。大概是根据点击、回复还有一个时间因素来处理。 目的是把最新的和最热的帖子推荐出来。 详细解释在这里:http://www.cnblogs.com/zhengyun_ustc/archive/2010/12/15/amir.html

    ruby 实现: https://github.com/hooopo/si9n/blob/master/lib/utils.rb