zoukankan      html  css  js  c++  java
  • 有趣的程序



    1.to_proc


    class Array
      def to_proc
        proc { |receiver| receiver.send *self }
      end
    end
    
    
    [ 'Hello', 'Goodbye' ].map &[ :+, ' BeiJing!' ]
    
    #=> ["Hello BeiJing!", "Goodbye BeiJing!"]
    


    receiver -> [ 'Hello', 'Goodbye' ]

    *self -> [ :+, ' BeiJing!' ]


    2.map(&:key)


    order_records = OrderRecord.all(:all, :limit => 10)
    
    order_records.map(&:order_no)
    
    #=> ["20140627141050-5096zG", "20140627143533-90bm9v", "20140627143648-2sJQxR", "20140627161046-UE0y07", "20140627161456-16GGT1", "20140627161707-58Lc3t", "20140627173915-0mI1Ji", "20140627175248-31YTtk", "20140627180802-s487cX", "20140717112957-eQ993g"]


    arr = '100+100+200+100'
    
    arr.split('+').map(&:to_i)
    
    


    3.hash = JSON.parse content.gsub!('=>', ':')


    require 'rubygems'
    require 'json'
    
    content = %Q({"credentials"=>{"token"=>"F538FF45937887AF7246E50928E0961F1", "refresh_token"=>"6EC400DD7B547B401D29474EA68952145", "expires_at"=>1418310654, "expires"=>true}})
    
    hash = JSON.parse content.gsub!('=>', ':')


    4.好有创意

    http://www.zcool.com.cn/special/znote2014/


    5.对 define_method。 instance_variable_get,instance_variable_set 的使用

    class Activity
      attr_reader :data
      
      def self.<< data
        new(data).call
      end
    
      def self.data_reader *attrs
        
        attrs.each do |attr|
          define_method attr do
            instance_variable_get("@#{attr}") || 
            instance_variable_set("@#{attr}", data[attr.to_sym])
          end
        end
        
      end
    
      def self.default attr, default
        orig_attr = "#{attr}_without_default"
        alias_method orig_attr, attr
        define_method attr do
          send(orig_attr) || default
        end
      end
    
      def self.set name, value
    
        if value.is_a?

    Symbol define_method name do instance_variable_get("@#{name}") || instance_variable_set("@#{name}", send(value) ) end elsif value.respond_to?

    :call define_method name do instance_variable_get("@#{name}") || instance_variable_set("@#{name}", value.call) end else define_method name do instance_variable_get("@#{name}") || instance_variable_set("@#{name}", value) end end end def initialize(data = {}) @data = data end def call '' end private def set(name,value) instance_variable_set("@#{name}", value) end def get_or_set(name) value = instance_variable_get("@#{name}") return value if value value = yield instance_variable_set("@#{name}", value) end end



    6. 编辑器

    http://hackerwins.github.io/summernote/





  • 相关阅读:
    HTTP 和 HTTPS
    HTTP 协议
    基础查询
    python编程从入门到实践笔记
    python-32-类的组合与初识继承
    python-31-初识面向对象与自定义类
    python-30-异常处理
    python-29-模块与包导入
    python-28-序列化模块
    python-27-其他常用模块(二)
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5066460.html
Copyright © 2011-2022 走看看