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
| # That damn GetPercentageRounds function I keep seeingin LinkedIn and Twitter | |
| # but in ruby, refactored, without telling why or whether is morally good or bad. | |
| # Just a function. | |
| def GetPercentageRounds(v) | |
| x = [10, [0, (10 * v).to_i].max].min | |
| '🔵' * x +('⚪️' * (10 - x)) | |
| end | |
| # irb(main):053:0> bar(0.1) | |
| # => "🔵⚪️⚪️⚪️⚪️⚪️⚪️⚪️⚪️⚪️" |
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 arraydiff(ref, other, key=[]) | |
| keyname = key.join('.') | |
| desc = [] | |
| (other - ref).each do |e| | |
| desc << "#{keyname}: Added '#{e}'" | |
| end | |
| (ref - other).each do |e| | |
| desc << "#{keyname}: Removed '#{e}'" | |
| end |
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
| #!/usr/bin/env ruby | |
| require 'uri' | |
| want_cmd = false | |
| args = ARGV.select do |a| | |
| if a == '--print' | |
| want_cmd = true | |
| false | |
| else | |
| true | |
| end |
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 'uri'; require 'cgi' | |
| def uri_extract_params(url_str) | |
| u = URI.parse(url_str) | |
| u.query.split('&').map { |x| x.split('=', 2).map { |c| c ? CGI.unescape(c) : nil } } | |
| end |
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
| r = Redis.new(url: ENV['REDIS_URL']) | |
| def fetch_all_keys(r, batch: 10_000, match: nil) | |
| cursor = '0' | |
| all_keys = [] | |
| loop do | |
| cursor, k = r.scan(cursor, count: batch, match: match) | |
| all_keys += k | |
| break if cursor == '0' | |
| end |
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
| ###################### | |
| ## Count estimate | |
| SELECT reltuples::bigint AS estimate FROM pg_class WHERE relname='table_name'; | |
| ###################### | |
| ## Table size in disk | |
| # source: https://stackoverflow.com/a/2596678/375888 |
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
| # Calculates sha256 HMAC signature for a given payload | |
| # Useful to check webhook signatures from e.g. github or stripe | |
| def calculate_signature(data, key, algorithm='SHA256') | |
| digest = OpenSSL::Digest.new(algorithm) | |
| hmac = OpenSSL::HMAC.new(key, digest) | |
| hmac << data | |
| hmac.hexdigest | |
| end |
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 unlolify(n);s=n.to_s(2);s.rjust((s.length/8.0).ceil*8,'0').split('').each_slice(8).to_a.map{|x|x.join.to_i(2).chr}.join;end | |
| def lolify(s);s.split('').map{|x|x.ord.to_s(2).rjust(8,'0')}.join;end | |
| # > unlolify(lolify('Hello, world!').to_i(2)) | |
| # => "Hello, world!" |
NewerOlder