Skip to content

Instantly share code, notes, and snippets.

View mrzasa's full-sized avatar

Maciek Rząsa mrzasa

  • Toptal
  • Rzeszów
View GitHub Profile
inputs = %w[
CollectionSelectInput
DateTimeInput
FileInput
GroupedCollectionSelectInput
NumericInput
PasswordInput
RangeInput
StringInput
TextInput
@mrzasa
mrzasa / gist:624135e254d4c981f01f
Last active August 29, 2015 14:16
Hash default and symbolize
h = {}
h[:a]
h.default = 0
h[:a] += 1
h = Hash.new(0)
require 'active_support/core_ext/hash/indifferent_access'
h = {"a" => 1, "b" => 2}
h.symbolize_keys
h.with_indifferent_access[:a]
@mrzasa
mrzasa / gist:cf189b681d39f73cc7b4
Last active August 29, 2015 14:18
Array wrap and splat
def print3(a,b,c)
puts "a: #{a}, b: #{b}, c:#{c}"
end
a = [1,2,3]
b = Array.new(3, 1)
c = Array.new(3) {|i| i*2}
print3(*a)
# This file is an implementation of multi-level statistics.
# Value object. It can be created basing on SQL output.
SingleSubmissionStats =
Struct.new(:shift, :retreat_type,
:retreat_level, :retreat_type_and_level_id,
:gender, :reserve, :status, :count)
# Vlass responsible for calculating various staatistics.
# It contains a stats array that can consist of SingleSubmissionStats
# This file contains shared examples that helps testing classes that has
# finder methods (e.g. ActiveRecord model classes).
#
# It accepts :expected, :others and :results params
# passed with let blocks:
#
# it_behaves_like :finder do
# let(:results) { ... }
# let(:expected) { ... }
# let(:others) { ... }
@mrzasa
mrzasa / devices.rb
Last active October 13, 2016 14:50
ActiveRecord::AssociationRelation vs ActiveRecord::Associations::CollectionProxy
> user.devices.all.uniq.class
=> Device::ActiveRecord_AssociationRelation
# so that we can chain more AR methods
> user.devices.all.uniq.limit(1)
=> [Device]
> u.devices.uniq.class
=> Array
# so we can only work on array:
> user.devices.uniq.limit(1)
@mrzasa
mrzasa / roo-percents-as-strings.xlsx
Last active February 17, 2017 11:28
Roo percent values
$ pry
[1] pry(main)> require "roo"
=> true
[2] pry(main)> require "roo/version"
=> true
[3] pry(main)> Roo::VERSION
=> "2.7.1"
[4] pry(main)> sheet = Roo::Spreadsheet.open("roo-percents-as-strings.xlsx", extension: "xlsx")
=> <#Roo::Excelx:1257514717731600 @tmpdir @shared @filename @sheet_files @sheet_names @sheets @sheets_by_name @options @cell @cell_type @cells_read @first_row @last_row @first_column @last_column @header_line>
[5] pry(main)> sheet.formatted_value('A', 1)
@mrzasa
mrzasa / benchmark.rb
Created September 10, 2018 14:25
textmaster-medium-regex-perfomance-benchmark
require "benchmark/ips"
PART = <<TEXT
A text with 123,21231,231,23d a number: 12,212,234,12
Other 12 43 123,21231,22,22,2,33d Some other 9012,123123 12
TEXT
TEXT = PART * 100
POSSESSIVE = /(\d++,?)++/
LAZY = /(\d+?,?)+?/
@mrzasa
mrzasa / benchmark-results.txt
Created September 10, 2018 14:30
textmaster-medium-regex-perfomance-benchmark-results
unrolling: 1299.0 i/s
possessive: 1146.9 i/s - same-ish: difference falls within error
greedy: 63.4 i/s - 20.48x slower
lazy: 61.8 i/s - 21.03x slower
@mrzasa
mrzasa / intesection.rb
Last active January 19, 2019 15:03
Find intersection of arrays
# arrays
result = []
# initial data
values = arrays.map{|a| a.next}
while true
maximal_value = values.max
for i 0...values.size do