Skip to content

Instantly share code, notes, and snippets.

View KamilLelonek's full-sized avatar
🏋️‍♂️
Do you even lift?

Kamil Lelonek KamilLelonek

🏋️‍♂️
Do you even lift?
View GitHub Profile
@KamilLelonek
KamilLelonek / CopyFileActivity.java
Created August 30, 2013 12:46
[Android] Activity used for copying selected files from anywhere on device to local data isolated storage.
package com.example.copy;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@KamilLelonek
KamilLelonek / module_function.rb
Created May 23, 2015 18:10
Ruby `module_function` example
module B
module_function
def b
puts 'b'
end
end
B.b # => 'b'
@KamilLelonek
KamilLelonek / 1_brand.rb
Last active September 15, 2022 16:05
Repository pattern example in Ruby for Rails developers
module Storage
module Models
class Brand < ActiveRecord::Base; end
end
end
@KamilLelonek
KamilLelonek / 1_forwardable.rb
Last active August 18, 2021 20:04
How to delegate methods in Ruby
require 'forwardable'
class LazyEmployee
extend Forwardable
def initialize(sandwich_maker)
@sandwich_maker = sandwich_maker
end
def_delegators :@sandwich_maker, :make_me_a_sandwich
defmodule PipelinexTest do
use ExUnit.Case, async: true
use Pipelinex
require Pipelinex
@text "Some random STRING."
describe "~>" do
test "should behave the same as the regular pipe operator" do
defmodule PipelinexTest do
use ExUnit.Case, async: true
alias Pipelinex.{Video, Rating}
describe "serialize" do
@rating1 %Rating{author: "James Bond", comment: "Quite nice."}
@rating2 %Rating{author: "Sherlock Holmes", comment: "Pretty good."}
@ratings [@rating1, @rating2]
@video %Video{title: "Iron Man 3", ratings: @ratings}
defmodule Pipelinex.Rating do
@enforce_keys ~w(author comment)a
defstruct @enforce_keys
def new(params), do: struct!(__MODULE__, params)
def encode(%__MODULE__{author: author, comment: comment}) do
%{
"author" => author,
"comment" => comment
defmodule Download do
use Pipe
def start(api_key) do
api_key
~> download()
~> parse()
end
defp download(123), do: {:ok, "valid result"}
@KamilLelonek
KamilLelonek / curry.rb
Last active November 20, 2019 23:14
Currying functions in Ruby
[1] (pry) main: 0> add = -> (a, b) { a + b }
=> #<Proc:0x007ffde11d72c0@(pry):1 (lambda)>
# Call proc with two arguments
[2] (pry) main: 0> add.(1, 2)
=> 3
# Call proc with one argument
[3] (pry) main: 0> add.(1)
ArgumentError: wrong number of arguments (1 for 2)
@KamilLelonek
KamilLelonek / 7_brand_in_memory_adapter.rb
Created January 19, 2015 20:01
Repository pattern spec example in Ruby for Rails developers
class BrandInMemoryAdapter
attr_accessor :db
def initialize
@db = []
end
def all
db
end