This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
# Returns a new hash with +self+ and +other_hash+ merged recursively. | |
# | |
# h1 = {:x => {:y => [4,5,6]}, :z => [7,8,9]} | |
# h2 = {:x => {:y => [7,8,9]}, :z => "xyz"} | |
# | |
# h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" } | |
# h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] } | |
def deep_merge(other_hash) | |
dup.deep_merge!(other_hash) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bundle exec rake db:drop RAILS_ENV=test | |
bundle exec rake db:create RAILS_ENV=test | |
bundle exec rake db:schema:load RAILS_ENV=test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = Article.find(3) | |
a[param] = "value" | |
a.save | |
# or | |
a = Article.find(3) | |
a.send("#{param}=", "value") | |
a.save |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://zh.wikipedia.org/wiki/%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA% | |
选择排序的交换操作介于 {\displaystyle 0} {\displaystyle 0}和 {\displaystyle (n-1)} (n-1)次之间。选择排序的比较操作为 {\displaystyle n(n-1)/2} n(n-1)/2次。选择排序的赋值操作介于 {\displaystyle 0} {\displaystyle 0}和 {\displaystyle 3(n-1)} 3(n-1)次之间。 | |
比较次数 {\displaystyle O(n^{2})} O(n^{2}),比较次数与关键字的初始状态无关,总的比较次数 {\displaystyle N=(n-1)+(n-2)+...+1=n\times (n-1)/2} N=(n-1)+(n-2)+...+1=n\times (n-1)/2。交换次数 {\displaystyle O(n)} O(n),最好情况是,已经有序,交换0次;最坏情况是,逆序,交换 {\displaystyle n-1} n-1次。交换次数比冒泡排序较少,由于交换所需CPU时间比比较所需的CPU时间多, {\displaystyle n} n值较小时,选择排序比冒泡排序快。 | |
原地操作几乎是选择排序的唯一优点,当空间复杂度要求较高时,可以考虑选择排序;实际适用的场合非常罕见。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found | |
private | |
def record_not_found | |
render plain "404 not found", status: 404 | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apt update && apt upgrade | |
apt install ruby vim git nodejs | |
apt install ruby-dev libxml2-dev libxslt-dev pkg-config make clang | |
gem install nokogiri -- --use-system-libraries | |
apt install libsqlite-dev | |
gem install sqlite3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DummyController < ApplicationController | |
def do | |
render json: { balance: 50 } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module A | |
def self.included(target) | |
v = target.instance_methods.include?(:method_name) | |
puts "in included: #{v}" | |
end | |
def self.append_features(target) | |
v = target.instance_methods.include?(:method_name) | |
puts "in append features before: #{v}" | |
super |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class @CubicEquations #加入 @ 跨作用域 绑定外层 this | |
constructor: (@a, @b, @c, @d) -> | |
@init() | |
@calc() | |
@result() | |
get_cube_root = (value) -> | |
if value < 0 | |
-Math.pow(-value, 1/3) | |
else if value > 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# source: https://apidock.com/ruby/Hash/delete | |
class Hash | |
def without(*keys) | |
cpy = self.dup | |
keys.each { |key| cpy.delete(key) } | |
cpy | |
end | |
end | |
h = { :a => 1, :b => 2, :c => 3 } |