This file contains hidden or 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 Foo | |
macro included | |
def method_{{@type.name.downcase.id}} | |
puts "I'm {{@type}}" | |
end | |
end | |
end | |
class Bar | |
include Foo |
This file contains hidden or 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 Foo | |
end | |
class Bar < Foo | |
end | |
a = [] of {Foo, String} | |
a << {Foo.new, "hi"} | |
a << {Bar.new, "hey"} |
This file contains hidden or 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 "ecr/macros" | |
record Person, name, age | |
class PersonView | |
getter person | |
def initialize(@person) | |
end |
This file contains hidden or 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 "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 |
This file contains hidden or 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 "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| |
This file contains hidden or 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 Array | |
def foo | |
if T.is_a?(Int32.class) | |
"Int!" | |
else | |
"Other!" | |
end | |
end | |
end | |
This file contains hidden or 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
(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 |
This file contains hidden or 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
macro mockable_method(type, method) | |
class {{type}} | |
def {{method.id}} | |
if _mock = @_mock_{{method.id}} | |
_mock.call | |
else | |
previous_def | |
end | |
end |
This file contains hidden or 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 "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 } |
This file contains hidden or 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
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) |