Skip to content

Instantly share code, notes, and snippets.

View novikserg's full-sized avatar

Sergei Novik novikserg

  • Amsterdam
View GitHub Profile
require "thwait"
class Job; end
module Processor
def self.perform(worker_count, jobs)
workers = worker_count.times.map do
Thread.new do
while job = jobs.pop
yield job
class Foo
MY_CONST = "hello".freeze
end
puts Foo::MY_CONST # "hello"
Foo.const_set("MY_CONST", "new const")
puts Foo::MY_CONST # "new const"
class Foo
def self.my_const
@novikserg
novikserg / test.rb
Last active March 15, 2018 18:40
Multiple attr_readers in Ruby
class FooBar
attr_reader :a, :b
def initialize
@a = 1
@b = 2
@c = 3
end
def see_the_c
@novikserg
novikserg / fibbonaci_iterate.exs
Last active February 21, 2018 07:21
A quick example of Fibbonaci sequence using Elixir's Stream (unlike eager Enums, Streams are lazy, generated 1 by 1 during execution, and highly composable)
def fibbonaci_iterate do
Stream.unfold([0, 1], fn [prev, curr] -> {curr, [curr, (prev + curr)]} end)
end
# fibbonaci_iterate |> Stream.take_every(10) |> Stream.map(fn(x) -> x * 2 end) |> Enum.take(5)
# [2, 178, 21892, 2692538, 331160282]
# note that the list was actually only enumerated once
class BuckFizz
def print
all.each do |buck_fizz|
puts buck_fizz
end
end
private
def all
class Triangle
attr_reader :a
attr_reader :b
attr_reader :c
def initialize(a, b, c)
@a, @b, @c = a, b, c
end
def type
# full description:
# https://codility.com/demo/take-sample-test/ps/
# tl;dr: Write a function that, given a zero-indexed non-empty array A consisting of N integers, returns the first covering prefix of A.
def solution(a)
last_unique = a.uniq.last
a.index { |el| el == last_unique }
end
class MyClass
def word
"word"
end
def go
word = word + "whatever"
word
end
end
class MyClass
def word
"word"
end
def go
word = word
word
end
end
class FacebookGraph
def initialize(token)
@graph = Koala::Facebook::API.new(token)
end
def profile
@graph.get_object('me')
end
def friends_ids