Ruby China
  • Topics
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • Sign Up
  • Sign In
李白
@jzlikewei
Member
NO. 11878 / 2014-03-04

[email protected]
11 Topics / 50 Replies
0 Followers
0 Following
0 Favorites
No GitHub.
  • Overview
  • Topics
  • Replies
  • Favorites
  • Following
  • Followers
  • [2022 年 3 月 22 日] Ruby Tuesday 线上聚会:如何找国外的远程工作? at March 11, 2022

    建议增加个议题:优缺点对比

  • WebSocket 方案选择 at May 10, 2019

    为什么不试试 go 呢?我们目前的做法是通过 go 来保持 go 长链接。

  • 散列和集合,为如下目的使用,哪个速度快? at May 10, 2019

    文档上写了“Set uses Hash as storage, so you must note the following points:”

  • 去他的 RTFM, 只要 @kayakjiang 我有问必答,绝不敷衍 at May 05, 2019

    ruby 我觉得推广难的两个原因:

    1. 没有简单好用的 ruby for windows。
    2. 过分强调 ruby on rails。Ror 对于新手真的是太不友好了,大量的 dsl 让人摸不着头脑。 但是但就 ruby 本身而言,ruby 的语法是很适合做新手推广和教学的(如果能解决 ruby for windows 就更好了)
  • Rails 里的时间 to_json 返回的格式问题 at August 25, 2017

    彻底解决了

    /active_support/core_ext/object/json.rb 这个文件给 Time 类加了 as_json 的函数:

    class Time
      def as_json(options = nil) #:nodoc:
        if ActiveSupport::JSON::Encoding.use_standard_json_time_format
          xmlschema(ActiveSupport::JSON::Encoding.time_precision)
        else
          %(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
        end
      end
    end
    

    具体在 Time 的 xmlschema 函数里会根据是否是 utc 时间来给出不同的格式,这里会直接 format,并不会调用 to_s

    def xmlschema(fraction_digits=0)
        fraction_digits = fraction_digits.to_i
        s = strftime("%FT%T")
        if fraction_digits > 0
          s << strftime(".%#{fraction_digits}N")
        end
        s << (utc? ? 'Z' : strftime("%:z"))
      end
    

    同样,关于 to_s 我也查看了对应的源代码(这部分代码写在 C 里,是拿不到 source 的),同样会根据是否是 utc 时间来返回不同的格式。

      static VALUE
    time_to_s(VALUE time)
    {
        struct time_object *tobj;
    
        GetTimeval(time, tobj);
        if (TIME_UTC_P(tobj))
            return strftimev("%Y-%m-%d %H:%M:%S UTC", time, rb_usascii_encoding());
        else
            return strftimev("%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding());
    }
    
  • Rails 里的时间 to_json 返回的格式问题 at August 25, 2017

    那篇文章我通过 google 搜到过,看过了,并不能解决我的疑问

    2.4.1 :005 > PlayRecord.last.created_at.method(:to_s).source_location
     => ["/Users/ilike/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.3/lib/active_support/core_ext/time/conversions.rb", 49] 
    2.4.1 :008 >   Time.now.method(:to_s).source_location
     => ["/Users/ilike/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.3/lib/active_support/core_ext/time/conversions.rb", 49] 
    2.4.1 :009 > Time.now.to_s
     => "2017-08-25 22:37:01 +0800" 
    2.4.1 :010 > PlayRecord.last.created_at.to_s
     => "2017-08-25 13:13:28 UTC" 
    2.4.1 :011 > PlayRecord.last.created_at.class==Time.now.class
     => true 
    
    
    

    源代码里的这部分是

    def to_formatted_s(format = :default)
       if formatter = DATE_FORMATS[format]
         formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
       else
         to_default_s
       end
     end
     alias_method :to_default_s, :to_s
     alias_method :to_s, :to_formatted_s
    

    两个一样的类,同一个函数,返回的结果是不一样的,这个让我有点迷惑

  • Rails 里的时间 to_json 返回的格式问题 at August 25, 2017

    我看了下 activesupport 中关于 time 的 源码 看起来也没有改动 to_s 的默认行为啊

    def to_formatted_s(format = :default)
        if formatter = DATE_FORMATS[format]
          formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
        else
          to_default_s
        end
      end
      alias_method :to_default_s, :to_s
      alias_method :to_s, :to_formatted_s
    
    
  • 使用 Goliath 1.0 实现高性能微服务 API 完整模板 at June 19, 2017

    我们一直在用 grape,裸 grape,跑在 unicorn 上,有想过用 goliath,但是没跑起来,赶时间就一直拖到现在。 可以介绍下如何跑在 goliath 上么

  • [上海][2016年11月1日] Ruby 聚会召集 at October 26, 2016

    +1

  • 请教各位大神个问题, create 的时候怎么跳过 before_create 呢 at August 30, 2016

    #3 楼 @13684054420

    class Employee
        before_create  :set_category
        def set_category
          self.employee_category = self.employee.category.display_name
        end
    end
    
    class EmployeeImport
        Employee.skip_callback(:create,:before,:set_category)
        Employee.create(hash)
        Employee.set_callback(:create,:before,:set_category)
    end
    
  • 请教各位大神个问题, create 的时候怎么跳过 before_create 呢 at August 30, 2016

    使用 skip_callback 和 set_callback

    Model.skip_callback(:create,:before,:do_something)
    Model.set_callback(:create,:before,:do_something)
    

    或者是这样

    class Writer < Person
               skip_callback :validate, :before, :check_membership, if: -> { self.age > 18 }
    end
    
  • Sequel 在做纯 API 项目时是不是比 ActiveRecord 更有优势? at April 12, 2016

    #19 楼 @ywjno 需要手动写 rake file,不过很简单的。 给你个参考 https://github.com/robbin/ruby_framework_bench/blob/master/grape_on_goliath/Rakefile (这个是 robbin 写的) 其实常见的 framework 都有 ActiveRecord 的相关库的: https://github.com/jhollinger/grape-activerecord How do I use ActiveRecord migrations?(Sinatra) http://www.sinatrarb.com/faq.html#ar-migrations

    这一部分其实我也只是一知半解,希望能有人来讲解下 rake file 的相关

  • Rails 中关于数据库管理的 rake task 是否可以单独的抽象出来 . at February 12, 2016

    这些还是要配合 ORM 来使用啊,就 ActiveRecord 来说,active_record/railties/databases.rake 这个里面就定义了一组 rake 命令

  • Bundler 到底是怎么工作的 (暨 Ruby 依赖管理历史回顾) at February 12, 2016

    #4 楼 @wppurking npm 与 ruby 的 gem 管理还不大一样吧。npm 默认是没有全局依赖的,所有的依赖都是本地的,nodejs 的 package 里面的依赖也是本地,所以应该不会出现版本冲突。

  • Model 中的 respond_to? 对于 render 的影响 / 利用 method_missing 完成 hash 字段在 form_for 中的使用。 at August 16, 2015

    #6 楼 @mueven 谢谢,解决疑惑了。

  • Model 中的 respond_to? 对于 render 的影响 / 利用 method_missing 完成 hash 字段在 form_for 中的使用。 at August 11, 2015

    #3 楼 @flemon respond_to_missing 是干嘛用的呢?

  • Model 中的 respond_to? 对于 render 的影响 / 利用 method_missing 完成 hash 字段在 form_for 中的使用。 at August 11, 2015

    #1 楼 @flemon

    s=Series.first
      Series Load (0.1ms)  SELECT  "series".* FROM "series"  ORDER BY "series"."id" ASC LIMIT 1
     => #<Series id: 2, name: "Over Lord", description: "装逼神作", information: {"location"=>"日本s", "start_time"=>"2015-07"}, created_at: "2015-08-10 17:10:53", updated_at: "2015-08-11 08:07:58">
    2.2.0 :002 > s.respond_to?(:info_location)
     => true
    2.2.0 :003 > s.method(:info_location)
    NameError: undefined method `info_location' for class `Series'
        from (irb):3:in `method'
        from (irb):3
    2.2.0 :004 > s.info_location
     => "日本s"
    2.2.0 :005 >
    

    删掉 respond_to? 之后,唯一的区别就是。s.respond_to?(:info_location) 的返回值为 false

  • [上海][2015年6月9日] Ruby 聚会召集 at June 08, 2015

    报名参加

  • 如何实现一个带权限控制的相册 at August 26, 2014

    #10 楼 @hhuai 这样算是 override 了 image_tag 这个函数么?

  • 如何实现一个带权限控制的相册 at August 25, 2014

    #7 楼 @hhuai send_file 怎么在 view 页面看呢?

  • 如何实现一个带权限控制的相册 at August 25, 2014

    #4 楼 @flowerwrong 和图片名称没关系。这样做只是让别人猜不到真实的图片地址,而不是我发给你个图片地址你打开后提示 403,或发你个相册你打开后提示相册时私有的。 #5 楼 @Rei 我研究下,云存储就算了,这个是个外包项目,没必要再增加成本(不过我用了阿里的 mysql 服务器,最低配的有个很大的优惠)

  • 如何实现一个带权限控制的相册 at August 25, 2014

    #1 楼 @hz_qiuyuanxin 我权限控制已经控制好了。我现在这这种,生成的头像图片地址是固定的,别人还是能直接通过访问地址来看图的。

  • 有支持多条件搜索的 GEM 吗? at August 19, 2014

    手写的哭了

  • ApplicationHelper 和 ApplicationController 的用法区别 at August 19, 2014

    对,ApplicationHelper 和 ApplicationController 在我的理解上其实就是逻辑的区别

  • [上海][2014年08月12日] Ruby Tuesday 活动召集 at August 11, 2014

    #13 楼 @iBachue 应该算是战前参谋部会议吧。。。

  • [上海][2014年08月12日] Ruby Tuesday 活动召集 at August 11, 2014

    诶?这个算什么?七周七语言之 Ruby 的预热么?

  • 有人用过 xmpp 的客户端吗 at August 01, 2014

    xmpp只是一套协议啊,具体实现有很多。Gtalk,人人聊天、微博的微信都是xmpp的协议,我大学期间用c#做过一个xmpp的客户端,用得是实现好的xmpp的类库

  • [上海] 七周七语言之 Ruby -2014年8月16日 at August 01, 2014

    报名听讲者

  • 好奇 ruby-china 最上方的进度条是怎么做出来的? at July 21, 2014

    #5 楼 @pynix 我顺带问过一次

  • 当全栈程序员是不是很累? at July 21, 2014

    自从公司给多了个做前端的,生活就美好多了....

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