花括号
puts (1..10).map {|i| i * 2}
2
4
Class Name corresponds to
Module included
Ensures that the object/expression is true.
Ensures that obj1 == obj2 is true.
/(?<foo>regexp)/ =~ "regexp" | |
foo # => "regexp" |
class Foo | |
include MagicEnum | |
attr_accessor :status | |
enum_attr :status, [ ["Open",0],["Close",1]] | |
end | |
foo = Foo.new | |
foo.status = 1 |
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) |
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)} |