Skip to content

Instantly share code, notes, and snippets.

View default_proc.rb
deep_hash = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
deep_hash[:a][:b][:c][:d] = 'Hello World'
puts deep_hash
# {:a=>{:b=>{:c=>{:d=>"Hello World"}}}}
@rayway30419
rayway30419 / default_proc.rb
Last active Mar 1, 2018
Hash#default_proc
View default_proc.rb
videos = Hash.new do |h,k|
h[k] = "https://www.youtube.com/watch?v=#{k.to_s}"
end
%w(vVyf_TqWVzI cZFPVAOrdXQ 5tQ4xHMR3iU).each { |id| videos[id] }
puts videos
# {
# "vVyf_TqWVzI"=>"https://www.youtube.com/watch?v=vVyf_TqWVzI",
# "cZFPVAOrdXQ"=>"https://www.youtube.com/watch?v=cZFPVAOrdXQ",
# "5tQ4xHMR3iU"=>"https://www.youtube.com/watch?v=5tQ4xHMR3iU"
# }
@rayway30419
rayway30419 / s1.rb
Created Dec 4, 2016
Effective ruby note #1
View s1.rb
# Ruby內除了nil以及false以外皆為真值
obj = 0
obj = ''
obj = {}
puts obj ? true : false
# true
View example.rb
module A
def init_ddb_client
Aws::DynamoDB::Client.new
end
def query params
client = init_ddb_client # Exception here
# loop for counting
end
end
@rayway30419
rayway30419 / aws-sdk.rb
Last active Jul 15, 2016
Dynamic configure AWS credentials on Rails (while you using IAM role for your instance)
View aws-sdk.rb
# config/initializers/aws-sdk.rb
# configure your credential at ~/.aws/credential
# [kdan]
# aws_access_key_id=your_access_key_id
# aws_secret_access_key=your_secret_access_key
if File.exist? File.expand_path('~/.aws/credentials')
Aws.config.update(
credentials: Aws::SharedCredentials.new(profile_name: 'kdan'),
)
end
You can’t perform that action at this time.