Skip to content

Instantly share code, notes, and snippets.

data:text/html, <style type="text/css">#e{position:absolute;top:0;right:0;bottom:0;left:0;}</style><div id="e"></div><script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script><script>var e=ace.edit("e");e.setTheme("ace/theme/monokai");e.getSession().setMode("ace/mode/ruby");</script>
@plusor
plusor / area.rb
Last active December 16, 2015 23:39
If you have only one area column in the table, and you want searches .
class Area < ActiveRecord::Base
acts_as_nested_set :counter_cache => :children_count
#Area.rebot(省ID,[市ID])
#Area.rebot(3198,[3213]) or Area.rebot(3198)
scope :robot, ->(s,q=[]) { where("parent_id in (?)",q.or(select(:id).where(:parent_id => s))) }
[:parent,:root,:province,:city,:district].each do |instance|
class_eval <<-EOF,__FILE__,__LINE__ + 1
delegate :name,:to => :#{instance} ,:allow_nil => true,:prefix => true
#{next if [:parent].include?(instance)}
@plusor
plusor / array.rb
Last active December 16, 2015 23:59
Deep create child hashes
class Hash
# k = {}
# k.create_deeps(*%w(1 2 3 4 5 6 7 8 9 10))
# #=> {"1"=>{"2"=>{"3"=>{"4"=>{"5"=>{"6"=>{"7"=>{"8"=>{"9"=>"10"}}}}}}}}}
def create_deeps(*args)
self.replace deep_create(*args)
end
def deep_create(*args)
dup.deep_create!(*args)
@plusor
plusor / foo.rb
Created May 10, 2013 09:22
Useful enum
class Foo
include MagicEnum
attr_accessor :status
enum_attr :status, [ ["Open",0],["Close",1]]
end
foo = Foo.new
foo.status = 1
@plusor
plusor / regexp.rb
Created June 17, 2013 03:55
Ruby 正则使用命名群组. 经常忘记格式怎么写
/(?<foo>regexp)/ =~ "regexp"
foo # => "regexp"
@plusor
plusor / alias vs. alias_method.md
Created July 29, 2013 14:21
alias 和 alias_method 区别 ruby
alias 和 alias_method 区别 ruby

1

class User

  def full_name
@plusor
plusor / Rails Assertions.md
Last active December 20, 2015 08:59
Rails Assertions

Ensures that the object/expression is true.

  • assert( boolean, [msg] )

Ensures that obj1 == obj2 is true.

  • assert_equal( obj1, obj2, [msg] )
@plusor
plusor / Ruby 全局变量常量.md
Created July 29, 2013 14:24
Ruby 全局变量/常量

异常信息(Exception Information)

                                     #传递给raise的异常对象.
$!                Exception          #The exception object passed to raise. [thread]
                                     #最后一个异常产生时的调用栈. 详见 Kernel#caller.
$@                Array              #The stack backtrace generated by the last exception. See Kernel#caller. [thread] 
@plusor
plusor / Ruby 回调方法.md
Created July 29, 2013 14:25
Ruby 回调方法

Class Name corresponds to

Module      included
@plusor
plusor / rake db 所有命令介绍.md
Created July 29, 2013 14:25
rake db 所有命令介绍
  • db:create creates the database for the current env
  • db:create:all creates the databases for all envs
  • db:drop drops the database for the current env
  • db:drop:all drops the databases for all envs
  • db:migrate runs migrations for the current env that have not run yet
  • db:migrate:up runs one specific migration
  • db:migrate:down rolls back one specific migration
  • db:migrate:status shows current migration status
  • db:migrate:rollback rolls back the last migration
  • db:forward advances the current schema version to the next one