This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
final List<List<int>> grid = [ | |
[1,2,3], | |
[4,5,6], | |
]; | |
// I thought this would fail to compile | |
// since grid[2] could be null: | |
print(grid[2][3]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lazy_demo(n, group_size:n, groups:n) | |
(1..Float::INFINITY) | |
.lazy | |
.map{|x| x * n } | |
.select{|product| product.even? } | |
.each_slice(group_size) | |
.map{|group| group.map(&:to_s).join(",") } | |
.first(groups) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationRecord | |
def self.random | |
max = maximum(primary_key) | |
return if max.nil? | |
find_random = lambda do | |
order(primary_key).find_by(primary_key => (rand(max)..)) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationRecord | |
def add_error(attribute, type, message=nil) | |
attribute = attribute.to_s.to_sym | |
type = type.to_s.to_sym | |
errors.add attribute, type, message: message | |
end | |
def has_error?(attribute, type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationRecord | |
def self.string_enum(column_name, values, **options) | |
enum column_name => values.map(&:to_s).index_by(&:itself), **options | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def deep_slice(schema) | |
self.class.deep_slice(self, schema) | |
end | |
def self.deep_slice(hash, schema) | |
nested_schemas = schema.last.is_a?(Hash) ? schema.last : {} | |
keys = schema.reject{|e| e.is_a?(Hash) } + nested_schemas.keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def shape | |
self.class.shape_of self | |
end | |
def self.shape_of(hash) | |
hash.sort_by{|k,v| k.to_s }.map do |k,v| | |
v = case v | |
when Array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "open3" | |
require "json" | |
class ShellCommand | |
attr_reader :line, :stdin, :binmode, :stdout, :stderr, :status | |
def initialize(line, stdin, binmode, stdout, stderr, status) | |
@line = line | |
@stdin = stdin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ValueObject | |
def ==(other) | |
case other | |
when ValueObject | |
self.object_value == other.object_value | |
else | |
false | |
end | |
end |
NewerOlder