This file contains 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
### 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 |
This file contains 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
puts "Hello" |
This file contains 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
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' |
This file contains 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
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) |
This file contains 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
$ ruby -ne 'if $_.split("\t").first.to_i == 1 then puts $_ else end' test.tsv > 1.tsv |
This file contains 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
sql = "LOAD DATA LOCAL INFILE '#{csv_file}' INTO TABLE tblname FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n'" | |
ActiveRecord::Base.connection.execute(sql) |
This file contains 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
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 |
This file contains 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
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 | |
} |
This file contains 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 QueueWithSingleStack | |
def initialize | |
@stack = [] | |
end | |
def enqueue(item) | |
@stack << item | |
end |
This file contains 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 QueueWithTwoStacks | |
def initialize | |
@s1 = [] | |
@s2 = [] | |
end | |
def enqueue(item) | |
@s1 << item | |
end |
OlderNewer