Ruby China
  • 社区
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • 注册
  • 登录
saiga
@saiga
会员
第 4375 位会员 / 2012-11-05

[email protected]
深圳
19 篇帖子 / 701 条回帖
14 关注者
1 正在关注
63 收藏
请认准唯一指定邮箱:aiasfina#hotmail.com
GitHub Public Repos
More on GitHub
  • 概况
  • 话题
  • 回帖
  • 收藏
  • 正在关注
  • 关注者
  • Class 和 Module at 2014年05月29日

    #2 楼 @ucooling java8 的接口...不过我没做过 java 不太清楚新特性

  • Class 和 Module at 2014年05月29日

    Java 的多态对应的是 duck typing Module 应该像 Java8 的胖接口(是这么叫?)

  • 求高效算法 at 2014年05月28日

    个人非常恶心这种面试题,都是捡大公司剩下的..

  • 求高效算法 at 2014年05月28日

    编程之美...http://www.cnblogs.com/jy02414216/archive/2011/03/09/1977724.html

  • 关于 rails cookies_serializer 设置项 at 2014年05月28日

    https://github.com/rails/rails/issues/15111

  • [求助] rails 版本 4.1,select 后的对象被保存在 association at 2014年05月28日

    找到了,是 automatic_inverse_of 搞的鬼。

    https://github.com/rails/rails/blob/923e274313dda8c772922e859df681f3d050a30e/activerecord/lib/active_record/reflection.rb#L466

    这里判断了是否开启 automatic_inverse_of

    def automatic_inverse_of
      if can_find_inverse_of_automatically?(self)
        inverse_name = ActiveSupport::Inflector.underscore(active_record.name).to_sym
    
        begin
          reflection = klass._reflect_on_association(inverse_name)
        rescue NameError
          # Give up: we couldn't compute the klass type so we won't be able
          # to find any associations either.
          reflection = false
        end
    
        if valid_inverse_reflection?(reflection)
          return inverse_name
        end
      end
    
      false
    end
    

    https://github.com/rails/rails/blob/923e274313dda8c772922e859df681f3d050a30e/activerecord/lib/active_record/associations/association.rb#L230

    然后这里返回了 nverse_name

    https://github.com/rails/rails/blob/923e274313dda8c772922e859df681f3d050a30e/activerecord/lib/active_record/associations/association.rb#L106

    结果导致 rails 执行了 set_inverse_instance

  • [求助] rails 版本 4.1,select 后的对象被保存在 association at 2014年05月28日

    #7 楼 @R_Rboys 在 ArticleContent 那加上 inverse_of : false 就好了。具体原因正在找

    class ArticleContent < ActiveRecord::Base
      belongs_to :article, inverse_of: false
    end
    
  • [求助] rails 版本 4.1,select 后的对象被保存在 association at 2014年05月28日

    #7 楼 @R_Rboys 还真是如此 = =....

  • 诸位看看我这样理解 rails 的用户登陆过程是不是正确的 at 2014年05月28日

    #9 楼 @so_zengtao no

  • [求助] rails 版本 4.1,select 后的对象被保存在 association at 2014年05月28日

    #4 楼 @hz_qiuyuanxin 4.0 -> 4.1rc -> 4.1.1

  • 样式加个 * 是几个意思 at 2014年05月28日

    ie css hack

  • [求助] rails 版本 4.1,select 后的对象被保存在 association at 2014年05月28日

    虽然不明白这个查询的意图,但是我这里是会重新查询一次的

    irb(main):010:0> a = ArticleContent.select(:article_id).first.article
      ArticleContent Load (0.8ms)  SELECT  "article_contents"."article_id" FROM "article_contents"   ORDER BY "article_contents"."id" ASC LIMIT 1
      Article Load (0.9ms)  SELECT  "articles".* FROM "articles"  WHERE "articles"."id" = $1 LIMIT 1  [["id", 1]]
    => #<Article id: 1, user_id: 1, title: "12", slug: "12", created_at: "2014-05-26 08:26:05", updated_at: "2014-05-26 08:32:10", category_id: 1, published_at: "2014-05-26 08:26:00">
    
    irb(main):011:0> a.content
      ArticleContent Load (1.0ms)  SELECT  "article_contents".* FROM "article_contents"  WHERE "article_contents"."article_id" = $1 LIMIT 1  [["article_id", 1]]
    => #<ArticleContent id: 2, article_id: 1, body: "<p>33</p><p><img src=\"/uploads/attachment/file/1/c...", created_at: "2014-05-26 08:43:53", updated_at: "2014-05-26 08:43:53">
    
    irb(main):012:0> Rails.version
    => "4.1.1"
    irb(main):013:0> 
    
    
  • 诸位看看我这样理解 rails 的用户登陆过程是不是正确的 at 2014年05月28日

    #6 楼 @so_zengtao token 只是拿来校验是否过期,当 token + id 给了服务器,服务器会到一张专门记 token 的表查询是否存在,不存在则忽略 id 直接清空 session,反之根据 id 拿到 user 对象

  • 用表还是是用配置文件 at 2014年05月28日

    表吧。文件并发写还要解决互斥问题

  • 诸位看看我这样理解 rails 的用户登陆过程是不是正确的 at 2014年05月27日

    不是. rails 常用的 devise 没看过源码不清楚。sorcery 和 @rei 在 campo 里自己写的登录逻辑都是记录 user_id。每次请求会自动用 user_id 查询 user 表。

  • 用 attr_reader 添加所有实例变量的方法? at 2014年05月27日

    #5 楼 @chenge 一开始是漏了 class 了.. 这样做没有安全问题,但有设计问题。ruby 默认遵循开放封闭原则,对象属性应该最小开放。

  • 用 attr_reader 添加所有实例变量的方法? at 2014年05月27日
    instance_variables.each do |var|
      self.class.class_eval do
        attr_reader var[1..-1]
      end
    end
    

    放到 initialize 方法最后,只针对单个方法中的实例变量,因为实例变量声明的位置不固定。

    另外,要这样做之前请确定你已经知道 ocp 原则和代码的用处,产品代码这么玩会被人问候的

  • 一个 Rails 新手的学习过程望朋友们多提建议 at 2014年05月27日

    #20 楼 @jasl scala...这个巨坑进去就出不来了....

  • Production 代码更新后如何重启 Puma at 2014年05月26日

    https://github.com/puma/puma#restart

    cat puma.pid | xargs kill -SIGUSR2
    
  • 发个同事 (一直搞 java) 刚刚写的代码,看看坑有多少 at 2014年05月26日

    #4 楼 @ane

    require './text.rb'
    

    or

    require_relative 'text.rb'
    
  • 发个同事 (一直搞 java) 刚刚写的代码,看看坑有多少 at 2014年05月26日

    #2 楼 @liwei78 在 ruby 里下划线开头的变量表示后面的程序不会用到,乱用会给人错觉的

  • [你还不用 pg?] 坑爹的 MySQL 事务 at 2014年05月23日

    #5 楼 @zj0713001 用乐观锁是否还是会有楼主提到的问题?mysql 给范围查询做了快照,版本信息是否也进去快照了?

  • ajax 高人快来,新手 $.ajax 调试晕菜了 at 2014年05月23日

    这样的描述别说高人,神仙也救不了

  • ruby 中循环的理解 at 2014年05月21日

    Enumerable#each 只要 mixin Enumerable 并实现 each 方法,就可以免费得到其余的 22 个方法。

    Implementing Enumerable: Write One Method, Get 22 Free

    class MultiArray
          include Enumerable
    
          def initialize(*arrays)
            @arrays = arrays
          end
    
          def each
            @arrays.each { |a| a.each { |x| yield x } }
          end
        end
    
        ma = MultiArray.new([1, 2], [3], [4])
        ma.collect                                        # => [1, 2, 3, 4]
        ma.detect { |x| x > 3 }                        # => 4
        ma.map { |x| x ** 2 }                             # => [1, 4, 9, 16]
        ma.each_with_index { |x, i| puts "Element #{i} is #{x}" }
        # Element 0 is 1
        # Element 1 is 2
        # Element 2 is 3
        # Element 3 is 4
    
  • ruby 中循环的理解 at 2014年05月21日

    这里是详细讲解了 Enumerable::Lazy,有兴趣的话可以看一下:http://railsware.com/blog/2012/03/13/ruby-2-0-enumerablelazy

  • ruby 中循环的理解 at 2014年05月21日

    不。Ruby 开始设计使用 block 并没有用到延迟迭代,这个好像到了 2.0 才引入。 这个设计初衷应该是向函数式靠拢:程序员不必关注如何迭代,而是如何处理逻辑(循环体)。

    2.0 引入了 Enumerable::Lazy ,让链式的迭代处理延延后。(PS: Ruby 的迭代都是基于 each 方法的)

    a = [1,2,3,4,2,5].lazy.map { |x| x * 10 }.select { |x| x > 30 } #=> 虽然同时出现 map和select,但each 一次也不会执行,程序返回迭代器
    a.to_a #=> [40, 50], #=> 这里才开始执行代码,而且只执行一次 each
    
  • 像这种网页命令行的效果是怎么实现的? at 2014年05月21日

    在 github 搜了一下 有些是用 js 写的解释器在前端直接将代码转换成 js 来执行。比如 ruby 到 js 可以用 Opal

  • 上一页
  • 1
  • 2
  • …
  • 12
  • 13
  • 14
  • 15
  • 16
  • …
  • 22
  • 23
  • 下一页
关于 / RubyConf / Ruby 镜像 / RubyGems 镜像 / 活跃会员 / 组织 / API / 贡献者
由众多爱好者共同维护的 Ruby 中文社区,本站使用 Homeland 构建,并采用 Docker 部署。
服务器由 赞助 CDN 由 赞助
iOS 客户端 / Android 客户端 简体中文 / English