Skip to content

Instantly share code, notes, and snippets.

View t11a's full-sized avatar

t11a t11a

  • Tokyo, Japan
View GitHub Profile
@t11a
t11a / sum_of_digits.rb
Created July 26, 2012 01:46
sum of digits
### solution 1
def sum_of_digits n
n.to_s.split('').inject(0) {|sum, i| sum + i.to_i}
end
### solution 2
def sum_of_digits n
sum = 0
while n > 0 do
d = n / 10
puts "Hello"
require 'aws-sdk'
AWS.config(
:access_key_id => 'your_access_key_id',
:secret_access_key => 'your_secret_access_key',
:s3_endpoint => 's3-ap-northeast-1.amazonaws.com' # see http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
)
s3 = AWS::S3.new
bucket_name = 'your_bucket/path/to'
timestamp = ActiveRecord::Base.connection.quote(Time.now.utc)
inserts = []
10**4.times do |i|
inserts << "('title#{i}', #{i}, #{i}, #{timestamp}, #{timestamp})"
end
sql = "INSERT INTO books (title, serial, price, created_at, updated_at) VALUES #{ inserts.join(',') }"
Book.connection.execute(sql)
@t11a
t11a / check_1st_elm_of_tsv.rb
Created March 1, 2013 07:49
tsvファイルの先頭の文字列をチェックしてヒットしたものを出力するワンライナー。 某プロジェクトで必要になったもの。
$ ruby -ne 'if $_.split("\t").first.to_i == 1 then puts $_ else end' test.tsv > 1.tsv
sql = "LOAD DATA LOCAL INFILE '#{csv_file}' INTO TABLE tblname FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n'"
ActiveRecord::Base.connection.execute(sql)
@t11a
t11a / ssh_config
Last active December 16, 2015 13:39
多段SSH、ssh/config
Host step_server
User username
Hostname xx.xx.xx.xx (Step Server's IP)
PreferredAuthentications publickey
IdentityFile ~/.ssh/private.key
Port 22
### ssh -Wオプションの場合(最近はこっちのほうがオススメらしい)
Host server_in_private
User username
require "rest_client"
def url_shortener(url)
res=""
key = "API_KEY_DAYO"
host = "https://www.googleapis.com/urlshortener/v1/url"
params = {
longUrl: url,
key: key
}
class QueueWithSingleStack
def initialize
@stack = []
end
def enqueue(item)
@stack << item
end
class QueueWithTwoStacks
def initialize
@s1 = []
@s2 = []
end
def enqueue(item)
@s1 << item
end