Skip to content

Instantly share code, notes, and snippets.

View BitOfUniverse's full-sized avatar

Alexey Nikolaev BitOfUniverse

View GitHub Profile
@BitOfUniverse
BitOfUniverse / main.go
Last active July 30, 2023 19:31
Go routines performance test
package main
import (
"fmt"
"runtime"
"sync"
"time"
)
type Job struct {
@BitOfUniverse
BitOfUniverse / hashing_example.rb
Created October 23, 2019 12:00
Consistent splitting using hashing and modulo
require 'zlib'
six_items = ["item_1", "item_2", "item_3", "item_4", "item_5", "item_6"]
ten_items = ["item_1", "item_2", "item_3", "item_4", "item_5", "item_6", "item_7", "item_8", "item_9", "item_10"]
chunks = 3 # should be set once and stay constant to preserve item<=>feed stickiness
six_items.each { |item_id| puts "#{item_id} goes to feed ##{Zlib.crc32(item_id) % chunks}" }
ten_items.each { |item_id| puts "#{item_id} goes to feed ##{Zlib.crc32(item_id) % chunks}" }
ten_items.reverse.each { |item_id| puts "#{item_id} goes to feed ##{Zlib.crc32(item_id) % chunks}" }
@BitOfUniverse
BitOfUniverse / implicit_block_capture.rb
Last active March 28, 2019 12:25
Ruby implicit block capture
def new(url = nil, options = nil)
block = block_given? ? Proc.new : nil
options = options ? default_connection_options.merge(options) : default_connection_options
Faraday::Connection.new(url, options, &block)
end
# And it should be used like this:
conn = Faraday.new(:url => 'https://www.smartly.io/developer') do |faraday|
faraday.request :url_encoded # form-encode POST params
@BitOfUniverse
BitOfUniverse / infinitely_nested_hash.rb
Created March 28, 2019 12:22
Ruby infinitely nested hash
hash = Hash.new {|h, k| h[k] = Hash.new(&h.default_proc) }
hash[:this][:is][:really] = 'Amazing!'
hash
=> {:this=>{:is=>{:really=>"Amazing!"}}}
@BitOfUniverse
BitOfUniverse / nested_hash.rb
Created March 28, 2019 12:12
Ruby nested hash
h = Hash.new {|h, k| h[k] = Hash.new }
@BitOfUniverse
BitOfUniverse / infinite_lambda_self_invocation.rb
Created March 28, 2019 12:10
Ruby infinite lambda self invocation
go_deeper.call.call.call.call.call.call.call.call
# You can try to implement it yourself. Your code will look like this:
(go_deeper = -> { puts "deeper"; go_deeper }).call.call.call.call.call
@BitOfUniverse
BitOfUniverse / return_multiple_values_example.rb
Created March 28, 2019 12:08
Ruby return multiple values example
return [:ok, response.body]
# or
[:ok, response.body]
@BitOfUniverse
BitOfUniverse / return_multiple_values.rb
Created March 28, 2019 12:07
Ruby return multiple values
status, reason = send def authorize
response = do_request
if response.success?
return :ok, response.body
else
return :error, response.error
end
end
@BitOfUniverse
BitOfUniverse / iffe_plus_finally_plus_return.rb
Created March 28, 2019 12:05
Ruby continue method execution with two return statements
send def escape
return "I want to return"
ensure
return "I want to finally get out"
end
=> "I want to finally get out"
@BitOfUniverse
BitOfUniverse / iffe_plus_finally.rb
Last active March 28, 2019 12:07
Ruby continue method execution
send def escape
return "I want to return"
ensure
puts "print this text first"
end
# Output looks like this:
print this text first
=> "I want to return"