Ruby China
  • Topics
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • Sign Up
  • Sign In
kb
@theblock24block
Member
NO. 17727 / 2015-03-21

london
0 Topics / 169 Replies
8 Followers
1 Following
55 Favorites
No GitHub.
  • Overview
  • Topics
  • Replies
  • Favorites
  • Following
  • Followers
  • 求一个 Rails 方式查询数据! at May 10, 2017

    类似拆订单那种业务?

  • 全栈笔记:每天推荐几篇优质的全栈开发文章 (以及我对全栈的看法) at May 09, 2017

    杂七杂八啥都有,好吧,是挺全栈的

    不过这条“Facebook 不可忽视的新功能”,不叫笔记吧

  • 求助缓存机制 at May 07, 2017

    这个无需什么特别技巧吧,只要满足两个条件即可:https://en.wikipedia.org/wiki/Pure_function

  • [介绍我的新 Gem] 范蠡:实现复杂业务逻辑的一种新方式 at May 06, 2017

    其实是看你哪部分代码更加常变,更加容易与他人冲突

    就像 java 的 aop,有人写在 xml,有人写在 model 的 annotation 上

  • Map for hash array using &: at April 30, 2017

    如果 LZ 只是想少写一些括号的话

    require 'active_support/core_ext/enumerable'
    
    [{aa: '123'}, {aa: '321'}].pluck :aa
    # => ["123", "321"]
    
  • 从一个大的 xml 文件中摘选出一部分内容,但是保持层级结构不变怎么实现 at April 29, 2017

    把信息抓完之后,用 erb 或 nokogiri 生成都可以吧

    require 'rexml/document'
    require 'rexml/streamlistener'
    require 'pp'
    
    CPU = Struct.new :name, :frequncy
    GPU = Struct.new :name, :benmark
    
    class Listener
      include REXML::StreamListener
    
      attr_reader :xml, :items
    
      def initialize
        @xml =[]
        @items = []
      end
    
      def tag_start name, attrs
        xml << name
      end
    
      def text t
        case xml 
        when ["computer", "cpu", "item", "name"]
          items << CPU.new(t) if t =~ /amd/i
        when ["computer", "cpu", "item", "frequncy"]
          items.last.frequncy = t if not items.empty? and CPU === items.last and not items.last.frequncy
        when ["computer", "gpu", "item", "name"]
          items << GPU.new(t) if t =~ /radeon/i
        when ["computer", "gpu", "item", "benmark"]
          items.last.benmark = t if not items.empty? and GPU === items.last and not items.last.benmark
        end
      end
    
      def tag_end name
        xml.pop
      end
    end
    
    listener = Listener.new
    
    parser = REXML::Parsers::StreamParser.new(File.new("/tmp/t.xml"), listener)
    
    parser.parse
    
    pp listener.items
    
  • 从一个大的 xml 文件中摘选出一部分内容,但是保持层级结构不变怎么实现 at April 29, 2017

    什么叫“总体结构不变”

  • has_one 的一个优化问题 at April 27, 2017

    没有分页吗?分页条件有没索引?外键有没索引?

    能否换成 B.has_one A, dependent: :nullify?然后只查 A.b_id is null,无需 join

  • Action Cable 一直报一个错 undefined method `identified_by' for ApplicationCable::Channel:Class at April 20, 2017

    你是想照着 rails guide 来做的吗

    class Connection < ActionCable::Connection::Base
        identified_by :current_user
    
  • 市面上需要怎样的程序员或者码工? at April 16, 2017

    从提到的经历来看,LZ 是最近几年追逐前端工具而迷失了吗?

  • 请教,如何在一个 model 中使用其它 model 里的查询条件…… at April 13, 2017
    def self.active
      where 'id in (?)', Post.recent.distinct.pluck(:user_id)
    end
    
  • 普通码农的思维总结 at April 01, 2017

    LZ 也不算普通吧

  • 有没有比较好的方法,去解析一个 block 里面的代码 at March 31, 2017

    执行 block 前,重写 method_added,然后 class_eval 那个 block,之后恢复 method_added

  • 我准备去一家毫无名气的初创公司工作了 at March 30, 2017

    北京?

  • 在 index 页面定义了删除的请求方式 ,但是日志还是走的 get 请求 找不到原因请小伙伴帮帮忙 at March 28, 2017

    挺想知道你说的“我是看着视频跟着一步一步操作的”的视频是?

  • 请教:如何在循环显示所有的 posts 时,并且显示当前用户对 post 的 comment 内容 at March 26, 2017

    不能吧

    joins 的话会出现重复 post 和漏 post(不是所有 post 都有 comment 和 user)

    includes 会查出多余 comment,之后还要在 ruby 中 filter

    还是说有什么特别写法?(我新手……)

  • 请教:如何在循环显示所有的 posts 时,并且显示当前用户对 post 的 comment 内容 at March 26, 2017

    是的

  • 请教:如何在循环显示所有的 posts 时,并且显示当前用户对 post 的 comment 内容 at March 26, 2017

    controller:

    @comments_by_curr_user = Comment.
        where(post_id: @post.ids). # 若有分页
        where(commenter: current_user).
        group_by(&:post_id)
    

    view:

    <%= @posts.each do |post| %>
        <%= post.name %>
        <% @comments_by_curr_user[post.id].each do |comment| %>
              partial .............
        <% end %>
    <% end %>
    
  • 怎么通过变量 去动态的取得 变量名? at March 24, 2017
    a1, a2, a3 = [:a, :b], [:c, :d], [:e, :f]
    
    (1..3).each do |i|
      binding.local_variable_get("a#{i}").each do |e|
        puts e
      end
    end
    
    (1..3).each do |i|
      eval("a#{i}").each do |e|
        puts e
      end
    end
    
  • rails db:migrate 后,莫名出现以前删除过的数据库字段 at March 21, 2017

    再 generate 一个删除字段的 migration?

  • [广州] 2017年2月 一荐好货业务加速、招兵买马!招聘 Ruby 工程师 3-5 人 at March 20, 2017

    还招吗

  • 关于 Chartkick 的问题 at March 08, 2017

    看看它所用的 js 库加载成功没有

  • 为什么 Rails 中没有依赖注入的概念? at March 04, 2017

    在 ruby 中,aop 可以简单地使用 alias_method/define_method/method_added 来实现,无需另外使用什么 InvocationHandler、cglib 那些繁复的东西

    DI、IOC 在 java 中很着重我想是为了提醒 javaer 把各种动作抽象成一个个 interface,但在 ruby 中无需管你什么类型,只要你有这个 method id 就能调用,也就是 duck typing

    ruby 本质上就是设计得这么动态

  • 关于元编程问题 at February 25, 2017

    class variables are bound at compile-time

  • 对 ActiveSupport::Concern 中 append_features 的疑问 at February 25, 2017

    为什么三次?

    Myc1.include Myc2; Mytest.inlcude Myc1; Mytest.include Myc2;

    理解是否正确?

    哪里有什么理解?

  • Rails 中 link_to 问题 at February 13, 2017

    他下面不是解释了吗

    fire off a DELETE /articles/:article_id/comments/:id to our CommentsController

  • 函数名容易与变量名冲突 at December 27, 2016

    如果函数是无需参数的,那就别用局部变量,每次都调用函数。

    如果该函数计算很耗性能,就用该函数所属类的实例变量缓存一下

    即使那个类不是你自己写的,也可以 prepend、extend 来制造缓存吧

  • 新手打算直接开始 ror,麻烦给个最新的链接 at December 07, 2016

    LZ 是还在上学的吗

  • 学习 Elixir 有什么新思路么? at November 25, 2016

    #2 楼 @gyorou 为什么说“除了 metaprogramming 那本”?

  • 元编程代码的解释 at April 27, 2016

    相等关系。setup 将你那两个 block,put one 和 put two,塞到@setups中。each_setup 再迭代@setups,将两个 block 传给{ |setup| setup.call},这才执行那两个 block。

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