Skip to content

Instantly share code, notes, and snippets.

View asterite's full-sized avatar

Ary Borenszweig asterite

  • NoRedInk
  • Buenos Aires, Argentina
View GitHub Profile
module Foo
macro included
def method_{{@type.name.downcase.id}}
puts "I'm {{@type}}"
end
end
end
class Bar
include Foo
class Foo
end
class Bar < Foo
end
a = [] of {Foo, String}
a << {Foo.new, "hi"}
a << {Bar.new, "hey"}
require "ecr/macros"
record Person, name, age
class PersonView
getter person
def initialize(@person)
end
require "socket"
class TCPServer
def initialize(port, backlog = 128)
@sock = C.socket(C::AF_INET, C::SOCK_STREAM, 0)
optval = 1
C.setsockopt(@sock, C::SOL_SOCKET, 2, pointerof(optval) as Void*, sizeof(Int32))
addr = C::SockAddrIn.new
require "http/server"
handler = ->(request : HTTP::Request) do
HTTP::Response.ok("text/plain", "ok\n")
end
middleware = HTTP::Server.build_middleware [
HTTP::LogHandler.new,
HTTP::ErrorHandler.new,
HTTP::WebSocketHandler.new do |ws|
class Array
def foo
if T.is_a?(Int32.class)
"Int!"
else
"Other!"
end
end
end
(Crystal::ModuleType+ | Crystal::CStructOrUnionType+ | Crystal::EnumType | Crystal::FunInstanceType)#lookup_first_def: 2
#is_restriction_of?: 2
Crystal::Type+#restrict: 2
#is_restriction_of?: 2
Crystal::Call?#not_nil!: 2
#is_restriction_of?: 2
#is_restriction_of?: 2
(Array(Crystal::External) | Array(Crystal::Def+))#each: 2
Crystal::Type+#is_restriction_of?: 2
Crystal::Type+#restrict: 2
macro mockable_method(type, method)
class {{type}}
def {{method.id}}
if _mock = @_mock_{{method.id}}
_mock.call
else
previous_def
end
end
require "iterator"
require "benchmark"
Benchmark.bm do |x|
x.report("Array#new") do
1_000.times { Array.new(100_000) { |i| i * 2 } }
end
x.report("times#map") do
1_000.times { 100_000.times.map { |i| i * 2 }.to_a }
struct Spec::Expect(T)
def initialize(@obj : T)
end
def to(expectation, file = __FILE__, line = __LINE__)
@obj.should(expectation, file, line)
end
def to_not(expectation, file = __FILE__, line = __LINE__)
@obj.should_not(expectation, file, line)