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
| abstract class Observer(T) | |
| abstract def notify(obj : T) | |
| end | |
| module Observable(T) | |
| def add_observer(observer : Observer(T)) | |
| observers = @observers ||= [] of Observer(T) | |
| observers << observer | |
| 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 "benchmark" | |
| def foo(&block) | |
| bar(&block) | |
| end | |
| def bar | |
| yield | |
| 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
| def with_pipe | |
| read, write = IO.pipe | |
| yield read, write | |
| ensure | |
| read.close if read rescue nil | |
| write.close if write rescue nil | |
| end | |
| with_pipe do |read, write| | |
| thread = Thread.new do |
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 "json" | |
| class Object | |
| macro def_find_by_name | |
| {{:macro.id}} def self.find_by_name(name) : {{@type.name.id}}.class | |
| case name | |
| \{% for subclass in {{@type.name.id}}.all_subclasses %} | |
| when \{{subclass.name}} | |
| \{{subclass.name.id}} | |
| \{% 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 "json" | |
| abstract class Plugin | |
| macro def self.find_by_name(name) : Plugin.class | |
| case name | |
| {% for subclass in Plugin.all_subclasses %} | |
| when {{subclass.name}} | |
| {{subclass.name.id}} | |
| {% end %} | |
| else |
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 "json" | |
| abstract class Plugin | |
| ALL = {} of String => Plugin.class | |
| macro inherited | |
| ALL[{{@type.name}}] = {{@type.name.id}} | |
| 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
| def foo(x : T) | |
| T::Baz | |
| end | |
| foo(1 || 1.5) |
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
| /usr/include/arpa/ftp.h:#define ERROR 5 /* permanent negative completion */ | |
| /usr/include/arpa/telnet.h:#define EL 248 /* erase the current line */ | |
| /usr/include/arpa/telnet.h:#define EC 247 /* erase the current character */ | |
| /usr/include/arpa/telnet.h:#define EOR 239 /* end of record (transparent mode) */ | |
| /usr/include/arpa/tftp.h:#define ERROR 05 /* error code */ | |
| /usr/include/arpa/tftp.h:#define EUNDEF 0 /* not defined */ | |
| /usr/include/arpa/tftp.h:#define ENOTFOUND 1 /* file not found */ | |
| /usr/include/arpa/tftp.h:#define EACCESS 2 /* access violation */ | |
| /usr/include/arpa/tftp.h:#define ENOSPACE 3 /* disk full or allocation exceeded */ | |
| /usr/include/arpa/tftp.h:#define EBADOP 4 /* illegal TFTP operation */ |
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 Tuple | |
| def +(other) | |
| Tuple.new *self, *other | |
| end | |
| end | |
| a = {1, 'a'} | |
| b = {"hey", 1.5} | |
| c = a + b | |
| puts c |
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 ThreadSafe(T) | |
| def initialize(@obj : T) | |
| @mutex = Mutex.new | |
| end | |
| macro method_missing(name, args, block) | |
| @mutex.synchronize do | |
| @obj.{{name.id}}({{*args}}) {{block}} | |
| end | |
| end |