This file contains hidden or 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
# this does not works. map called once. | |
p [1..10].map{|i|rand(9)}.join('') | |
# > "1" | |
# this works. | |
p [*1..10].map{|i|rand(9)}.join('') | |
# > "5012242033" | |
# difference is [*1..10] and [1..10] | |
# "*" expands Range object to list, but both are "Array" Class. strange... |
This file contains hidden or 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 "openssl" | |
key = "123" | |
base_string = "abc" | |
hash = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, key, base_string) | |
p [hash].pack('m').gsub(/\n/, '') |
This file contains hidden or 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 "cgi" | |
p CGI.escape("abc+&%") | |
# "abc%2B%26%25" |
This file contains hidden or 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
def get_query_string(query_hash) | |
query_array = [] | |
query_hash.each{|key, val| val ? query_array.push([key, val].join('=')) : nil} | |
query_string = query_array != [] ? query_array.join('&') : '' | |
end | |
query_hash = {"a"=> 1, "c" =>nil, "b"=>3} | |
p get_query_string(query_hash) |
This file contains hidden or 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
hash1 = {:b => 1, :a => 2} | |
# => {:b=>1, :a=>2} | |
Hash[hash1.sort_by{|key, val|key}] | |
# => {:a=>2, :b=>1} | |
Hash[hash1.sort] | |
# => {:a=>2, :b=>1} | |
# sort by value | |
Hash[hash1.sort_by{|key, val|val}] | |
# => {:b=>1, :a=>2} |
This file contains hidden or 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
hash1 = {:a=> 1, :b=>2} | |
hash2 = {} | |
hash2[:b] = 3 | |
p hash2 | |
hash2.update(hash1) | |
p hash2 | |
#{:b=>3} |
This file contains hidden or 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 AdminHandler(webapp.RequestHandler): | |
@util.login_required | |
def get(self): | |
csrf_key = self._generate_csrf_key() | |
# pass csrf key as template parameter | |
@util.login_required | |
def post(self): | |
if users.is_current_user_admin(): | |
if not self._check_csrf_key(self.request.get('csrf_key')): |
This file contains hidden or 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 -*- | |
# simple generator function | |
def generator1(limit): | |
for count in range(limit): | |
yield count | |
# simple generator function with return | |
def generator2(limit, stop): | |
for count in range(limit): | |
if count > stop: |
This file contains hidden or 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
def gen1(): | |
count = 0 | |
while True: | |
count = count + 1 | |
print count | |
if count > 10: | |
return | |
if count == 5: | |
print 'raise!' | |
raise '5!!!' |
This file contains hidden or 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 -*- | |
import tweepy | |
consumer_token = '' | |
consumer_secret = '' | |
access_token = '' | |
access_secret = '' | |
def get_tweets(api, screen_name): |