Skip to content

Instantly share code, notes, and snippets.

View ktanaka101's full-sized avatar

Kentaro Tanaka ktanaka101

  • Japan
  • 13:18 (UTC +09:00)
View GitHub Profile
Int32.new("+1") #=> 1
Int32.new("-1") #=> -1
"+1".to_i #=> 1
"-1".to_i #=> -1
type_id = 0b0001
flags = [false, true, false, true]
(type_id & 0x0F) << 4
((type_id & 0x0F) << 4).to_u8 |
(flags[3] ? 0x8_u8 : 0x0_u8) |
(flags[2] ? 0x4_u8 : 0x0_u8) |
(flags[1] ? 0x2_u8 : 0x0_u8) |
(flags[0] ? 0x1_u8 : 0x0_u8)
0xFFFFFFFF
0b11111111111111111111111111111111
0xFF_FF_FF_FF
0b11111111_11111111_11111111_11111111
# crystal
enum Abc : UInt8
A = 0x00
B = 0x01
C = 0x02
end
Abc::A + 2 #=> C : Abc
module MyInterface
end
class MyClass
include MyInterface
end
MyClass.new.is_a?(MyClass) #=> true
MyClass.new.is_a?(MyInterface) #=> true
[MyClass.new].is_a?(Array(MyClass)) #=> true
class MyProtocol
module MyInterface
end
def initialize(@clients : Array(MyInterface))
end
end
class MyClient
include MyProtocol::MyInterface
# crystal
module Interface
def a
end
end
class Cls1
include Interface
end
# crystal
class ABC
end
def ABC.new
false
end
ABC.new #=> false
# crystal
a = [1, 2, 3]
b = [1, 2, 3]
a == b #=> true
a === b #=> true
a = {1, 2, 3}
b = {1, 2, 3}
a == b #=> true
a === b #=> true
# crystal
{"a" => 1, "b" => 2} #=> {"a" => 1, "b" => 2} : Hash(String, Int32)
a = {"a": 1, "b": 2} #=> {a: 1, b: 2} : NamedTuple(a: Int32, b: Int32)
{"a" => 1, "b": 2} #=> {"a" => 1, "b" => 2}
{"a": 1, "b" => 2} #=> Syntax error in :6: space not allowed between named argument name and ':'