Ruby China
  • Topics
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • Sign Up
  • Sign In
@quakewang
VIP
NO. 162 / 2011-11-22

[email protected]
上海
26 Topics / 752 Replies
212 Followers
4 Following
22 Favorites
GitHub Public Repos
  • rfcs 1

    This repository contains proposals, standards and documentations related to Nervos Network.

  • linux 1

    Linux kernel source tree

  • cfn-node 0

  • ckb 0

    CKB is a public/permissionless blockchain, the layer 1 of Nervos network.

  • ractor 0

    Rust actor framework

  • autocorrect 0

    A linter and formatter for help you improve copywriting, to correct spaces, punctuations between ...

  • ckb-vm 0

    CKB's vm, based on open source RISC-V ISA

  • ckb-auth 0

    A consolidated library featuring numerous blockchains authentication techniques on CKB-VM

  • hyprnote 0

    Privacy-first AI Notepad for back-to-back meetings

  • fiber-scripts 0

More on GitHub
  • Overview
  • Topics
  • Replies
  • Favorites
  • Following
  • Followers
  • Ruby 版 Leetcode,已水完 100 题,求同好 Review at July 20, 2015

    #23 楼 @arth 我用的查表法... 156ms

    ROMANS = [%w(I II III IV V VI VII VIII IX),
              %w(X XX XXX XL L LX LXX LXXX XC),
              %w(C CC CCC CD D DC DCC DCCC CM),
              %w(M MM MMM) + [''] * 6]
    
    def int_to_roman(num)
      r, i = '', 0
      while num != 0 do
        r = ROMANS[i][num % 10 - 1] + r unless num % 10 == 0
        num /= 10
        i += 1
      end
      r
    end
    
  • Ruby 版 Leetcode,已水完 100 题,求同好 Review at July 17, 2015

    #10 楼 @arth 所以我说是投机呀 ^_^

  • Ruby 版 Leetcode,已水完 100 题,求同好 Review at July 17, 2015

    #7 楼 @lgn21st 对比一下其他语言的解法,从代码行数来说,ruby 绝对是开挂的

  • Ruby 版 Leetcode,已水完 100 题,求同好 Review at July 17, 2015

    另外 leetcode 的测试代码大部分是来自 C 的,ruby 的数字没有超界问题,有一些就可以投机了 比如 Factorial Trailing Zeroes,就可以一行解掉:

    def trailing_zeroes(n)
        (1..13).to_a.map{|i| n / 5 ** i}.inject(:+)
    end
    

    还可以利用强大的内置对象,比如 count primes,require 一下,也是一行解掉

    require 'prime'
    def count_primes(n)
        Prime.each(n - 1).to_a.size
    end
    

    ^_^

  • Ruby 版 Leetcode,已水完 100 题,求同好 Review at July 17, 2015

    再抽了几个题目看一下,我觉得你写的代码太不像 ruby 了...

    你的 https://github.com/acearth/LeetCodePractice/blob/master/isomorphic.rb 18 行

    可以用 hash 的 uniq keys 来判断,3 行解掉:

    def is_isomorphic(s, t)
        h = {}
        s.chars.each_with_index{|c, i| h[c] ||= t[i]; return false if h[c] != t[i]}
        return h.keys.uniq.size == h.values.uniq.size
    end
    

    https://github.com/acearth/LeetCodePractice/blob/master/compareVersion.rb 30 多行

    可以用 ruby 的数组<=>操作,8 行解掉:

    def compare_version(version1, version2)
        v1 = version1.split('.').map(&:to_i)
        v2 = version2.split('.').map(&:to_i)
        if v1.size > v2.size
            v2 += [0] * (v1.size - v2.size)
        elsif v2.size > v1.size
            v1 += [0] * (v2.size - v1.size)
        end
        v1 <=> v2
    end
    
  • Ruby 版 Leetcode,已水完 100 题,求同好 Review at July 17, 2015

    Happy Number 你的解法,116ms

    def is_happy(n)
      a=n.to_s
      a=a.to_s
      k=0
      while true
        ss=a.to_s
        d=0
        ss.length.times do |i|
          di=ss[i].to_i
          d+=di**2
        end
        a=d
        return true if d==1
        k+=1
        return false if k>100
      end
    end
    

    多用 hash,96ms

    def is_happy(n)
        hash = {}
        while hash[n].nil?  do
           n = hash[n] = n.to_s.chars.inject(0){|m, c| m + c.to_i * c.to_i}
        end
        n == 1
    end
    
  • Ruby 版 Leetcode,已水完 100 题,求同好 Review at July 17, 2015

    看了第一题 Two Sum 你的解法 800 多 ms

    def two_sum(nums, target)
     pro=Hash.new
      nums.length.times do|i|
        pro[nums[i]]=i+1
      end
      nums.length.times do|i|
        k=target-nums[i]
        if nums.include? k
          if(i+1 != pro[k])
            return [i+1,pro[k]]
          end
        end
      end
    end
    

    加个 hash 来记录,性能会好很多,80 多 ms

    def two_sum(numbers, target)
        hash = numbers.map.with_index.to_h
        found = numbers.find.with_index do |n, index| 
          target_index = hash[target - n] and target_index != index
        end
        [numbers.index(found) + 1, hash[target - found] + 1].sort
    end
    
  • Ruby 中的 @ 符号在定义 method 里是什么意思? at July 09, 2015

    是一元运算符(Unary Operator) 比如你在 irb 里面执行 -[1, 2, 3] 会得到错误 NoMethodError: undefined method `-@' for [1, 2, 3]:Array

    你可以给 Array 定义这个一元运算符:

    class Array
      def -@
        self.reverse
      end
    end
    -[1,2,3]
    # => [3, 2, 1] 
    
  • 寻求优雅的代码, 计算两个日程是否有冲突? at July 09, 2015

    有现成轮子可以用啊

    require 'ice_cube'
    s1 = IceCube::Schedule.new(Time.parse('2015-07-08 16:00:00'), duration: 3600)
    s2 = IceCube::Schedule.new(Time.parse('2015-07-06 16:00:00'), duration: 1800) do |s|
      s.add_recurrence_rule(IceCube::Rule.daily)
    end
    s1.conflicts_with?(s2)
    #=> true
    
  • RubyConf China 2015 将于 10月10日-11日 于深圳举办 (内附讲师征集链接) at July 06, 2015

    手工点赞

  • 【提问】请教一个 SQL 字符串转换成数组的方法 at June 23, 2015

    试试看这个 sql parser https://github.com/cryodex/sql-parser

    基于 racc 生产的 parser,复杂 sql 无法解析的话,自己修改 racc 文件。

    不过,我还是没明白这样的需求实际用途是什么...

  • 咨询小公司网络配置方案 at June 17, 2015

    70~80 平米的房间是放不下 30 人的...

    我们之前 30 个人,用的实用面积大概有 100 平米吧,已经觉得蛮挤了

    我们是配置了 3 台无线路由 (NETGEAR R6300) + 1 台入门级思科 24 口交换机。内网服务器不一定要用服务器,普通家用 PC 就可以了,做好备份即可。

  • Active Record 中如何使用 delegate 到一个不相关的 class 里的方法 at June 16, 2015

    也可以直接用 AR 的 Single table inheritance

    class Order < ActiveRecord::Base
    end
    
    class OrderFoo < Order
      def pay
      end
    end
    
    class OrderBar < Order
      def pay
      end
    end
    
    create_table :orders do |t|
      t.string :type
    end
    
  • Rails 中能把 created_at, updated_at 字段名字改了吗? at June 12, 2015

    monkey patch ActiveRecord::Timestamp 的 timestamp_attributes_for_create / timestamp_attributes_for_update 方法

    https://github.com/rails/rails/blob/master/activerecord/lib/active_record/timestamp.rb#L93

    将 [:created_at, :created_on] 替换成 [:create_time]

  • Redis 实战之薄荷 timeline 的优化 at June 08, 2015

    动态用主动拉的话,直接查询动态表不行吗?

    select * from activities where user_id in (select user_id from followers where follower_id = CURRENT_USER_ID) and id > LAST_VIEW_ID
    
  • [上海] 蝉游记 / 携程周末 招聘 Ruby 工程师 at June 02, 2015

    #5 楼 @kgen #6 楼 @ashchan 秘诀是多写代码,多运动 ^_^

  • 这个东西哪里有卖? at June 01, 2015

    3d 打印一个吧

  • [上海] 蝉游记 / 携程周末 招聘 Ruby 工程师 at June 01, 2015

    #1 楼 @cqcn1991 是的,最近因为事情多了,所以要再招个 ruby 开发

  • 发布生产环境时,由于资源使用的是 UPYUN,自动化部署 at May 21, 2015

    upyun 现在不是有 cdn 功能吗?在 upyun 那边配置一个源地址就可以了。

  • Rails 中自动布署工具 mina 的经验谈 at May 12, 2015

    mina 和 capistrano 相比,速度快非常多,现在我们一些小项目都用它,1~3 台服务器的规模,不过大规模部署的话比 cap 有一些缺点:

    不支持多台服务器并行部署,得手写一个 task,串行调用

    set :domains, %w[10.xx.xx.3 10.xx.xx.4 10.xx.xx.5]
    task :deploy_all do
      isolate do
        domains.each do |domain|
          set :domain, domain
          invoke :deploy
          run!
        end
      end
    end
    

    而实际上有些任务只需要在一台服务器上运行,比如 db migrate,比如 asset precomplie(有 CDN 的情况),考虑到这些情况,手写的 task,就变成和 cap 类似 role 的样子了。还要考虑到如果其中一台部署出错,要 rollback 之前已经部署过的服务器,脚本就更加复杂了。服务器数量如果大于 5 台的话,一台一台部署,速度上并没有比 cap 的并行部署来得快。

    不支持 gateway 的方式,解决方法是在 gateway 上执行 mina,而不是直接在客户端执行。

    用户量比 cap 少,会遇到一些生产中的 bug,比如这个 unicorn 的插件,在这个 commit 之前没有考虑到 gem file 改变的情况: https://github.com/scarfacedeb/mina-unicorn/commit/e7c331ab4566eff149a4ffabde73b8ba6b1fa202

    会导致 gem file 改变以后,无法链接到最新的,导致 unicorn 重启失败。

    不过在小规模运用上 mina 还是完胜 cap

  • 用什么方法输入一个地址,然后计算出坐标呢? at May 06, 2015

    做过类似的,输入 POI 的名字,用地图 api 搜索出相关的地点,显示在地图上,用户点击具体点,如果确认无误,经纬度入库既可以。

  • 审核功能如何实现?简单一个 state 字段?还是有更复杂的东西? at May 05, 2015

    如果状态只有审核和非审核,通常我会用时间:published_at,默认创建出来是 published_at 是 nil,审核过了,设置为 Time.now

    如果有 2 种以上的状态,可以考虑用 state machine

  • 大家有没有好的在 Rails 里用的 UBB Gem 推荐推荐 at April 30, 2015

    discuz 和 phpbb 用的是一样的 tag 吗?试试看这个 https://github.com/cpjolicoeur/bb-ruby

  • Visual Studio Code for Mac, Linux 来了 at April 30, 2015

    安装尝鲜了一下。

    1. Git 集成做得非常好
    2. js、html、css 代码提示很完善 3.在 MacBook Air 和 Intel NUC 上运行很流畅
    3. 暂时不支持 ruby...
  • 安装和配置 Postfix at April 23, 2015

    #4 楼 @rei 你用的 mailgun 服务不是固定 IP,是和他人共享的,就会有这个问题,升级到支持固定 IP 套餐可以解决,SPF 和 DKIM 都支持。

  • 其实我们应该记得时常 gem update --system at April 23, 2015

    经常用 rvm 更新 ruby 版本的,顺带就自动更新 gem 版本了

  • 国外公司账号汇款到国内个人账号需要交税嘛 at April 22, 2015

    不交税,只要一次没超过限额,按汇率换算成 RMB 好像是 1 万(可能不准确),就直接入账了

  • 如何用 Ruby 实现 OAuth 服务端 at April 17, 2015

    https://github.com/doorkeeper-gem/doorkeeper

  • Data Warehouse Schema Design at April 13, 2015

    #8 楼 @hooopo 明白了。文章写得很棒。

    Mondrian 也可以不用那个 jruby gem 的,直接用他的集成环境就可以了。时间纬度没有到分、秒的,确实直接 sql 就够了。

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