Ruby China
  • Topics
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • Sign Up
  • Sign In
Rei
@Rei
Admin
NO. 1 / 2011-10-28

[email protected]
深圳
182 Topics / 9132 Replies
727 Followers
0 Following
11 Favorites
中下水平 Rails 程序员
Reward
GitHub Public Repos
  • writings 940

    [Closed] Source code of writings.io

  • alipay 732

    Unofficial alipay ruby gem

  • code_campo 291

    [Closed] Source code of http://codecampo.com

  • asciidoctor-pdf-cjk-ka... 101

    **no longer maintained**

  • asciidoctor-htmlbook 31

    Asciidoctor HTMLBook is an Asciidoctor backend for converting AsciiDoc documents to HTMLBook docu...

  • material-ui 17

  • rich-text-editor 12

  • htmlrenderer 12

  • rails-chatgpt-demo 8

  • rails-app 7

    A Rails project template lets me start new projects quickly.

More on GitHub
  • Overview
  • Topics
  • Replies
  • Favorites
  • Following
  • Followers
  • Google 产品全面撤销 CNNIC 根证书 at April 02, 2015

    干得好!早该管管了!

  • Google 产品全面撤销 CNNIC 根证书 at April 02, 2015

    好,支持,威武,有希望了!

  • Debugging memory leaks in Ruby at April 01, 2015

    👍

  • open-uri 中的 open 方法如何设置 timeout at April 01, 2015

    http://ruby-doc.org/stdlib-2.1.2/libdoc/timeout/rdoc/Timeout.html

  • 申请以及集成 Stripe 的 Alipay 支付方案 at March 31, 2015

    #17 楼 @chunlea 成功了要分享一下啊。

  • 利用 ActiveSupport::Notifications 在 Rails 中实现 PUB/SUB 模式 at March 31, 2015

    ActiveSupport::Notifications 真实应用场景我只见过 rack-attack 用来记日志

    https://github.com/kickstarter/rack-attack#logging--instrumentation

  • 我能说 Rails 的 Assets 的各种机制就是一 XX 吗? at March 31, 2015

    #7 楼 @lyfi2003 赞,Rails 把最佳实践都默认了,以至于我都不知道最佳体现在哪里。

  • 当我们在谈论类的时候我们在谈论什么 at March 31, 2015

    #9 楼 @emanon 我没说过“单一职责”是指一个类只能有一个方法。

    什么是一件事,什么是太多关系,这些就够好好扯淡的了。模式的好处是让人知道它的存在,坏处就是总是在谈论它的存在。

  • 我能说 Rails 的 Assets 的各种机制就是一 XX 吗? at March 31, 2015

    #4 楼 @hanluner 我没碰过“无厘头”的问题。

  • 我能说 Rails 的 Assets 的各种机制就是一 XX 吗? at March 31, 2015

    assets 是给希望把前端代码提升到一等公民地位的人用的,你把别人写好的模版文件整个导入,自己一点不想碰,并不想给予一等公民地位,当然不合适了。

    解决方案:放到 public 里。

  • 当我们在谈论类的时候我们在谈论什么 at March 31, 2015

    "单一职责"就是个伸缩尺,根据需要放大缩小。按照定义,Rails 的 Controller 就是不是单一职责的,因为每个 Controller 居然承担了某个资源的全部 CRUD,有时还有 Member / Collection 操作。那么这还要不要拆呢,这时候可以说服自己,CRUD 都属于操作单一资源,所以这是单一职责,于是心安了。看,这是不是文字游戏。标准库里面的类到底承担了多少职责,这时候应该选择视而不见。

    当然也有贯彻落实“单一职责”,并把它作为设计哲学的人,于是搞出这样的代码:

    class Show
      include Lotus::Action
    
      def call(params)
        @article = Article.find params[:id]
      end
    end
    

    这就是 Lotus 框架 http://lotusrb.org/ ,我刚看到的时候费解为什么会有人想写这种代码啊。别误解我,我觉得这个框架挺好的,喜欢这类风格的人就可以跟着去了,免得祸害 Rails。

    后来读了 Steve Yegge 的《名词王国里的执行》(Execution in the Kingdom of Nouns),发现原来真的有人把 Java 社区批判过的东西当宝捡起来。这里的 call 不就履行了 execute 的职责吗?

    TopicCommentCreator.new(topic, comment_params).execute 好,topic.comments.create(params) 坏。 —— 程序猿庄园戒律

    得了吧,我编程的时候才不想着什么原则什么模式。如果一个类承担了太多工作,那就 提炼类;如果一个类做得事情太少了,那就 内联化类。要学习整理代码的技术,《重构》是本好书,它列出了一大堆模式,但不会列一堆框框条条让你一定要遵守,要具体情况具体分析。

    当你一直往上把事情弄得太抽象,就会像上太空一样没有氧气。有时候这些聪明的思想家就是停不下来,然后就创造出这些荒唐又无所不包的高层次宇宙景像,这些东西什么都好,就是完全没有实际的意义。 —— Joel Spolsky,别让架构太空人吓到你

    PS:新手应该多研究代码,少谈些模式,我推荐一个项目: https://github.com/rubygems/rubygems.org 。

  • CentOS 7 无法安装 gem install charlock_holmes -v '0.6.9.4' at March 30, 2015

    #2 楼 @string2020 命令行输入 g++ 看看,没有的话说明不包含在你安装的包内。

  • CentOS 7 无法安装 gem install charlock_holmes -v '0.6.9.4' at March 30, 2015

    g++: Command not found

    编译工具缺失,Ubuntu 通常是安装 build-essential 这个包把一波编译工具装上,Centos 不了解。

  • 发布 / 订阅模式 at March 30, 2015

    Rails 有 ActiveSupport::Notifications http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html

  • Guides 中文版怎么不能访问了? at March 29, 2015

    是指这里么? http://guides.ruby-china.org/

    原因看这里 https://ruby-china.org/topics/24890

  • AngularJS 为何选择了 TypeScript,而不是 Coffee 呢? at March 29, 2015

    你是 @mogodb 马甲么?你能停止 Troll 行为么?

    http://zh.wikipedia.org/wiki/%E7%99%BD%E7%9B%AE

  • 不用数据库用什么? at March 27, 2015

    YAML

  • alipay 0.6.0 和 alipay-demo 项目 [已发布] at March 27, 2015

    #7 楼 @huobazi 担保交易的异步消息最长会到用户确认收货的时候,也就是 10 天后。之前 alipay gem 只能用一个 key 去校验小心,所以需要一种方案支持多帐号。

  • alipay 0.6.0 和 alipay-demo 项目 [已发布] at March 27, 2015

    #5 楼 @huobazi 我自己的需求的是想换一个商户帐号。

    支付宝的平台接口我一直没搞明白意思。

  • nginx 和 passenger 的 nginx.conf 配置问题请教 at March 26, 2015

    原文没有 location 这个配置块。

    在每个配置块 passenger 配置会清空,所以我怀疑是加了这个配置块导致的问题。

  • alipay 0.6.0 和 alipay-demo 项目 [已发布] at March 26, 2015

    还有个问题我还没想好,不知道还有谁碰到这个需求:支持使用多个商户帐号 https://github.com/chloerei/alipay/issues/27

  • 还是 Turbolinks 的问题 at March 24, 2015

    节省的不是下载时间,是解析执行时间。

    https://ruby-china.org/topics 这个页面

    无 Turbolinks:

    有 Turbolinks:

  • Unicorn 里创建线程 at March 24, 2015

    Ruby 线程有 GIL,可能与这个有关。

  • Ruby 有没有类似 Python 中 ImageFilter 的 gem 包 at March 23, 2015
    system :python, 'path/to/script.py'
    
  • Rails 有类似 J2EE 中的 value object 吗? at March 23, 2015

    从基础说起,可以用一个 Hash 对应一个 Person,一个 Person 数组就是:

    persons = [ { name: '李xx', sex: '', tel: '' }, { name: '张', sex: '', tel: '' } ] # and more
    

    数组(Array)内含了不少方法,其中的 inject 方法可以将数组遍历然后计算出一个结果,文档看这里 http://ruby-doc.org/core-2.2.1/Enumerable.html#method-i-inject ,对应你的需求就是:

    persons.inject(Hash.new(0)) {|hash, person| hash[person[:name][0]] += 1; hash }
    # => { '李' => 1, '张 => 1 }
    

    解释一下,Hash.new(0) 创建一个空 Hash 用来存放结果(这个 Hash 每个 key 的默认值是 0),然后传到 inject block 里面;block 里面的 person[:name][0] 取到用户的姓,然后作为 key 找到 hash 的对应值 += 1;最后 block 要将 hash 作为结果返回作为下一次迭代的 hash 值,遍历完毕后 hash 就是你要的结果。

    如果你希望 Person 成为一个类,方法差不多,代码变为:

    class Person
      # attr_accessor 定义了 getter setter 方法
      attr_accessor :name, :sex, :tel
    
      def initialize(attributes)
        @name = attributes[:name]
        @sex = attributes[:sex]
        @tel = attributes[:tel]
      end
    end
    
    persons = [ Person.new( name: '李xx', sex: '', tel: ''), Person.new( name: '张xx', sex: '', tel: '') ]
    
    persons.inject(Hash.new(0)) {|hash, person| hash[person.name[0]] += 1; hash }
    

    以上是 Ruby 的做法,你问到了 Rails 的做法,那么假设数据是存在数据库里的,并且使用了 ActiveRecord,那么可以通过数据库查询得出:

    class Person < ActiveRecord
    end
    
    # PostgreSQL 语法
    Person.select('substring(name for 1) as first_name, count(*)').group('first_name').map{ |person| [person.first_name, person.count] }.to_hash
    
  • 发布一个 Gem —— ActiveService,给 Rails 添加一个 Service 层 at March 23, 2015

    #16 楼 @aptx4869 有时不止需要一个对象。

  • 大家最开始学 RoR 的时候,每本教程都读了几遍?是如何入门的呢? at March 23, 2015

    学习一项技能要花多少时间? http://chloerei.com/2013/12/12/how-long-does-it-take-to-learn-a-skill/

  • 发布一个 Gem —— ActiveService,给 Rails 添加一个 Service 层 at March 23, 2015

    #10 楼 @flowerwrong 继承 railtie 可以实现以下目标:

    • creating initializers
    • configuring a Rails framework for the application, like setting a generator
    • adding config.* keys to the environment
    • setting up a subscriber with ActiveSupport::Notifications
    • adding rake tasks

    http://api.rubyonrails.org/classes/Rails/Railtie.html

    目前只利用了它自动添加 lib/rails 目录下的 generator 这个功能。

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