Skip to content

Instantly share code, notes, and snippets.

@iansrc0811
iansrc0811 / fibonacci_dynaminc.rb
Last active October 18, 2017 14:47
ruby 費式數列(fibonacci), 使用動態規劃法
# 複雜度 O(n)
input = gets
def fib_dy(n, f = [0, 1])
if n >= f.length
for i in f.length..n
f[i] = f[i-1] + f[i-2]
end
end
return f[n]
@iansrc0811
iansrc0811 / devise_authentication_api.rb
Created June 29, 2017 15:43 — forked from skozz/devise_authentication_api.rb
Authenticate your API with Devise gem, token by header. Ruby on Rails 4. Read the comments.
#Session controller provides a token
#/controllers/api/sessions_controller.rb
class Api::SessionsController < Devise::SessionsController
before_filter :authenticate_user!, :except => [:create]
before_filter :ensure_params_exist, :except => [:destroy]
respond_to :json
def create
resource = User.find_for_database_authentication(:email => params[:user_login][:email])
return invalid_login_attempt unless resource
@iansrc0811
iansrc0811 / db.json
Last active June 15, 2017 01:51
json-db.json
{
"clockInCode": { "clockInCode": "MjAxNy0wNi0xNSAwOTozMTowNiArMDgwMA==\n" }
}
@iansrc0811
iansrc0811 / closure_example.js
Last active March 9, 2017 09:21
簡單的closure範例
//可以把 閉包( closure) 想成是在函式執行完畢後,
//讓你「記住」並持續存取一個函式 scope (範疇) 的變數 的一種方式
function makeAdder(x) {
function add(y){
return y + x;
}
return add;
}
@iansrc0811
iansrc0811 / books_crawler.rb
Last active June 25, 2019 07:53
爬博客來書名的爬蟲
require 'rubygems'
require 'nokogiri'
require 'mechanize'
require 'open-uri'
class Books_Crawler
def get_book_names(book_name)
book_item = self.start_crawler(book_name)
book_names = []
@iansrc0811
iansrc0811 / capybara cheat sheet
Created November 13, 2016 13:37 — forked from zhengjia/capybara cheat sheet
capybara cheat sheet
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')