• Hi,我是另一个账号😂

  • #19 楼 @pynix 我是 react 党,但我不反对用 jquery 来处理一下 dom 😄

  • 文章最后不敢苟同,尤其是最后一段代码

    #encoding:utf-8
    
    require "./nancy"
    $b = Nancy::Base.new
    
    $b.get '/' do
      'Hey there!'
    end
    
    $b.post '/login' do
      # do something
    end
    

    实用环境这样才是合理的,然而,只是多打三个字符,我却觉得比元编程方式更清楚自己在干什么

  • 几道有趣的 leetcode 题目 at 2015年08月19日

    下班前刚好刷到了 majority_element 这题,解法一模一样,我管它叫 fast and simple and cheat 另外贴一下三分之一那个问题的解法,正巧是从 @luikore 那里学到的:

    def majority_element(nums)
      nums.sort.chunk{|x|x}.map(&:last).inject([]){|m,v| m << v[0] if v.size > nums.size / 3;m}
    end
    
  • 依我的理解,Ruby 的 return 是用来中断方法执行的,类似 exit 之于线程、break 之于循环 方法默认就有返回值,所以除了用来控制流程,其他情况完全可以不写

  • Ruby char ASCII 相互转换 at 2015年05月24日

    字符串可以试试 pack/unpack

    [104, 101, 108, 108, 111].pack 'c*'
    'hello'.unpack 'c*'
    
  • 我也来一个,请无视逻辑部分

    $score = 0
    $changed = false
    
    lines  = []; 4.times { lines << [nil, nil, nil, nil] }
    
    helper        = -> {
                          puts "┌───────────────┐"
                          puts "│ Use your keyborad.           │"
                          puts "│ W: Up A:Left S:Down D:Right  │"
                          puts "│ Return: Next step            │"
                          puts "└───────────────┘"
                        }
    
    score         = -> {
                          puts "┌───────────────┐"
                          puts "│Score:        #{"%016d" % $score}│"
                          puts "└───────────────┘"
                        }
    
    header        = -> { puts "┌───┬───┬───┬───┐" }
    block         = -> n { "_#{"_" * (4 - n.to_s.length)}#{n}_│" }
    liner         = -> a { s = "│" ; a.each {|i| s += block.call i }; puts s}
    footer        = -> { puts "└───┴───┴───┴───┘" }
    
    new_num = -> {
                    loop do
                      x, y = rand(4), rand(4)
                      if lines[x][y].nil?
                        lines[x][y] = rand < 0.8 ? 2 : 4
                        break
                      end
                    end
                  }
    
    start = -> { 2.times { new_num.call } }
    
    restart = -> {
                    loop do
                      puts "Play again? (Y/N)"
                      str = gets
                      case str.rstrip.to_sym
                      when :Y, :y
                        lines.clear; 4.times { lines << [nil, nil, nil, nil] }
                        $score = 0; new_num.call
                        break
                      when :N, :n; exit; end
                    end
                  }
    
    game_over     = -> {
                          puts "┌───────────────┐"
                          puts "│          Game Over!          │"
                          puts "└───────────────┘"
                          restart.call
                        }
    
    congrulations = -> {
                          puts "┌───────────────┐"
                          puts "│Congrulations!You've got 2048!│"
                          puts "└───────────────┘"
                          restart.call
                        }
    
    refresh = -> { score.call; header.call; lines.each {|l| liner.call l }; footer.call}
    
    complement = -> l { (4 - l.size).times { l << nil } if l.size < 4 }
    
    arrange = -> line {#, size {
                              case line.size
                              # when 0, 1
                              when 2
                                if line[0] == line[1]
                                  line[0] += line[1]; line[1] = nil; $score += line[0]
                                end
                              when 3
                                if line[0] == line[1]
                                  line[0] += line[1]; line[1], line[2] = line[2], nil; $score += line[0]
                                elsif line[1] == line[2]
                                  line[1] += line[2]; line[2] = nil; $score += line[1]
                                end
                              when 4
                                if line[0] == line[1]
                                  line[0] += line[1]; line[1], line[2], line[3] = line[2], line[3], nil; $score += line[0]
                                end
                                if line[1] == line[2]
                                  line[1] += line[2]; line[2], line[3] = line[3], nil; $score += line[1]
                                end
                                if !line[2].nil? && line[2] == line[3]
                                  line[2] += line[3]; line[3] = nil; $score += line[2]
                                end
                              end
                            }
    
    process = -> args {
                        rows = []
                        if args[0]
                          rows = lines
                        else
                          4.times { |i| rows << [ lines[3][i], lines[2][i], lines[1][i], lines[0][i] ] }
                        end
                        rows.each do |row|
                          row.reverse! if args[1]
                          row.compact!
                          arrange.call row
                          complement.call row
                          row.reverse! if args[1]
                          4.times { |i| lines[3][i], lines[2][i], lines[1][i], lines[0][i] = rows[i] } unless args[0]
                        end
                      }
    
    left  = -> { process.call [ true, false] }
    down  = -> { process.call [false, false] }
    right = -> { process.call [ true,  true] }
    up    = -> { process.call [false,  true] }
    
    check = -> {
                  lines.each { |l| l.each { |b| congrulations.call if b == 2048 } }
                  size = 0; lines.each { |line| size += line.compact.size }
                  game_over.call if size == 16
                }
    
    game = -> {
                helper.call
                start.call
                loop do
                  $nums  = 0
                  image = []; lines.each { |l| image << l.clone }
                  refresh.call
                  str = gets
                  case str.rstrip.to_sym
                  when :a; left.call
                  when :s; down.call
                  when :d; right.call
                  when :w; up.call;
                  else next; end
                  check.call
                  new_num.call if image != lines
                end
              }
    
    game.call
    
  • The Moneywrap at 2013年12月11日

    点赞