Skip to content

Instantly share code, notes, and snippets.


Max Chernyak maxim

Block or report user

Report or block maxim

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View barewords.rb
def plain_text_receipt # => Runtime: 5 sec, Network Timeout
Thank you for your order!
Product: #{name} - #{price} # => 2 DB queries
Tax: #{tax_amount} # => 1 DB query, 1 API call
Total: #{total_amount} # => 2 DB queries, 1 API call
maxim / react.rb
Created Jun 28, 2019
Extending react-rails with styled components support
View react.rb
# config/initializers/react.rb
Rails.configuration.react.server_renderer = do
def render_from_parts(before, main, after)
js_code = compose_js(before, main, after)
View stream_ext.ex
defmodule StreamExt do
@doc """
Stream records from Ecto in batches.
Supported options are
* batch_size - how many rows to fetch at once
* strategy - either :offset or :id
* :offset - uses SQL offset to fetch pages of results
This is slower, but works for any query.
maxim / docker-compose.yml
Created Aug 7, 2017
Getting started with docker
View docker-compose.yml
version: '3'
image: postgres:9.6
- "5432:5432"
- postgres-data:/var/lib/postgresql/data
View select_at.ex
def select_at(list, indexes),
do: select_at(list, indexes, 0, []) |> Enum.reverse
def select_at([], _, _, results),
do: results
def select_at(_, [], _, results),
do: results
def select_at([item | list], [i | indexes], i, results),
do: select_at(list, indexes, i + 1, [item | results])
def select_at([_ | list], indexes = [index | _], i, results),
do: select_at(list, indexes, i + 1, results)
View singleton_or_not.rb
# You could have an object for the whole app to use (e.g. in rails initializer)
ChanceGenerator =
# But also easily testable (this would be in a test setup):
test_chance = 1)
# The constructor can have a little documentation:
class Chance
# If seed is omitted, a new seed will be generated for each random call
def initialize(seed: nil)
maxim / 1-works_fine.rb
Last active Nov 28, 2016
Strange ruby bug: can't put comment after linebreak in method args. Tested in 2.3.2.
View 1-works_fine.rb
def foo \
a: 'a',
# comment here is ok
b: 'b'
puts a, b
foo(a: 'a', b: 'b') # => a\nb
View pipe.rb
class Object
def pipe(meth, obj = self, args = [])
obj.public_send(meth, self, *args)
class Doubler
def double(c);{|v| v*2} end
View foobar.rb
module Widgets
def self.extended(base)
class << base
attr_accessor :widgets
base.widgets = {}
def add_widget(name, widget)
maxim / ecto_batch_stream.ex
Last active Oct 1, 2019
Similar to Rails `find_each`, but for Elixir's Ecto, using Stream
View ecto_batch_stream.ex
defmodule EctoBatchStream do
import Ecto.Query, only: [from: 1, from: 2]
@batch_size 1000
# Example:
# query = from u in MyApp.User, select:
# stream =, query)
# stream |> Stream.take(3) |> Enum.to_list # => […]
You can’t perform that action at this time.