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 'csv' | |
headers = %w(name age gender) | |
people = [ | |
['alice',14,'female'], | |
['bob',32,'unknown'], | |
['carol',17,'female'] | |
] | |
csv_string = CSV.generate('', write_headers: true, headers: headers) { |csv| |
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
# coding: utf-8 | |
CSV.open('people.csv').each do |row| | |
# 何かしらの処理 | |
end | |
CSV.open('people.csv', 'a') do |csv| | |
csv.flock(File::LOCK_EX) | |
people.each do |person| | |
csv << person |
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
# coding: utf-8 | |
require 'set' | |
a = %w(a b c d).to_set | |
b = %w(c d e f).to_set | |
# どちらかに含まれる要素の集合(和集合) | |
p a | b # >> #<Set: {"a", "b", "c", "d", "e", "f"}> | |
p a.union(b) # >> #<Set: {"a", "b", "c", "d", "e", "f"}> |
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 'set' | |
a = %w(Alice Bob Charlie).to_set | |
b = %w(Alice Bob).to_set | |
p a.superset?(b) # >> true | |
p b.subset?(a) # >> true | |
p a.proper_superset?(b) # >> true | |
p b.proper_subset?(a) # >> true |
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
# coding: utf-8 | |
require 'time' | |
# Time#parseはTimeオブジェクトを返す | |
Time.parse('2011-04-01 16:01').class # => Time | |
# 様々な形式の時刻を表す文字列をTimeオブジェクトに変換する | |
Time.parse('2011-04-01 16:01') # => 2011-04-01 16:01:00 +0900 | |
Time.parse('2011/04/01 16:01') # => 2011-04-01 16:01:00 +0900 | |
Time.parse('2012-01-29 14:13:08 +900') # => 2011-04-01 16:01:00 +0900 |
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
# coding: utf-8 | |
require 'date' | |
# 年月日から日付オブジェクトを得る | |
date = Date.new(2011,4,1) # => #<Date: 2011-04-01 ((2455653j,0s,0n),+0s,2299161j)> | |
# 日付を指定した書式の文字列で表現する | |
date.strftime('%Y/%m/%d') # => "2011/04/01" | |
date.strftime('%Y/%m/%d %H:%M:%S') # => "2011/04/01 00:00:00" |
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
# coding: utf-8 | |
require 'date' | |
# 年月日からDateTimeオブジェクトを得る | |
date = DateTime.new(2011,4,1,4,4,23) | |
# => #<DateTime: 2011-04-01T04:04:23+00:00 ((2455653j,14663s,0n),+0s,2299161j)> | |
# 日時を指定した書式の文字列で表現する | |
date.strftime('%Y%m%d %H:%M:%S') # => "20110401 04:04:23" |
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 'rack' | |
class RackApplication | |
def call(env) | |
[200, {'Content-Type' => 'text/plain'},['Hello!']] | |
end | |
end | |
run RackApplication.new |
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 'rack' | |
class RackApplication | |
def call(env) | |
request = Rack::Request.new(env) | |
response = if request.path_info == '/' | |
body = "#{request.request_method}: Hello! #{request.params['name']}!" | |
Rack::Response.new(body, 200, {'Content-Type' => 'text/plain'}) | |
else | |
Rack::Response.new('Not Found',404,{'Content-Type' => 'text/plain'}) |
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 'rack' | |
class RackApplication | |
def call(env) | |
[200, {'Content-Type' => 'text/plain'},['Hello!']] | |
end | |
end | |
use Rack::ShowStatus | |
use Rack::Auth::Basic do |username,password| |