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
@maxim
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 =
Class.new(React::ServerRendering::BundleRenderer) do
private
def render_from_parts(before, main, after)
js_code = compose_js(before, main, after)
@context.eval(js_code)
end
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
maxim / docker-compose.yml
Created Aug 7, 2017
Getting started with docker
View docker-compose.yml
version: '3'
services:
postgres:
image: postgres:9.6
ports:
- "5432:5432"
volumes:
- 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 = Chance.new
# But also easily testable (this would be in a test setup):
test_chance = Chance.new(seed: 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
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
end
foo(a: 'a', b: 'b') # => a\nb
View pipe.rb
class Object
def pipe(meth, obj = self, args = [])
obj.public_send(meth, self, *args)
end
end
class Doubler
def double(c); c.map{|v| v*2} end
end
View foobar.rb
module Widgets
def self.extended(base)
class << base
attr_accessor :widgets
end
base.widgets = {}
end
def add_widget(name, widget)
@maxim
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: u.email
# stream = EctoBatchStream.stream(MyApp.Repo, query)
# stream |> Stream.take(3) |> Enum.to_list # => […]
@maxim
maxim / 1-ru.rb
Last active Nov 9, 2015
If ruby was in Russian
View 1-ru.rb
затребовать 'shellwords'
затребовать 'posix/spawn'
затребовать 'skeptick/error'
модуль Skeptick
класс Command
атр_читатель :shellwords
опр инициализировать(shellwords)
@shellwords = shellwords
You can’t perform that action at this time.