Skip to content

Instantly share code, notes, and snippets.

View 3014zhangshuo's full-sized avatar
🎯
Coding

张硕 3014zhangshuo

🎯
Coding
View GitHub Profile
@3014zhangshuo
3014zhangshuo / rails_deep_merge.rb
Created August 25, 2018 04:20
rails deep merge method
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)
@3014zhangshuo
3014zhangshuo / example.txt
Created August 4, 2018 03:49
rails reset test env database
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
@3014zhangshuo
3014zhangshuo / solution.rb
Created August 2, 2018 02:51
Dynamic set object attributes
a = Article.find(3)
a[param] = "value"
a.save
# or
a = Article.find(3)
a.send("#{param}=", "value")
a.save
@3014zhangshuo
3014zhangshuo / description.txt
Last active August 1, 2018 03:20
Select sort ruby version
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值较小时,选择排序比冒泡排序快。
原地操作几乎是选择排序的唯一优点,当空间复杂度要求较高时,可以考虑选择排序;实际适用的场合非常罕见。
@3014zhangshuo
3014zhangshuo / controller_exception_handler.rb
Last active July 31, 2018 23:51
rails controller exception handler
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
@3014zhangshuo
3014zhangshuo / termux.sh
Created July 31, 2018 01:51 — forked from paresharma/termux.sh
Rails on Termux
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
@3014zhangshuo
3014zhangshuo / dummy_controller.rb
Created July 23, 2018 09:36 — forked from psobocinski/dummy_controller.rb
Asserting on a JSON response in a Rails controller test with MiniTest
class DummyController < ApplicationController
def do
render json: { balance: 50 }
end
end
@3014zhangshuo
3014zhangshuo / implementation.rb
Created July 11, 2018 02:14 — forked from paneq/implementation.rb
Append features vs included
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
@3014zhangshuo
3014zhangshuo / cubic_equations.coffee
Last active April 19, 2018 10:01
一元三次方程解
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
@3014zhangshuo
3014zhangshuo / hash_without.rb
Created March 26, 2018 03:59
ruby hash without method
# 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 }