Skip to content

Instantly share code, notes, and snippets.

View ktanaka101's full-sized avatar

Kentaro Tanaka ktanaka101

  • Japan
  • 14:51 (UTC +09:00)
View GitHub Profile
3.0 / 2.0
3.0 // 2.0
3.0 / 2
3.0 // 2
3 / 2
3 // 2
3 / 2.0
if true
a = 10
else
b = 20
end
p a #=> 10
p b #=> nil
# crystal
str1 = "
if true
10
else
20
end
"
str2 = <<-STR
# crystal
a = <<-INPUT
10
INPUT #=> syntax ok
b = {
<<-INPUT
10
INPUT, 10
} #=> syntax error
# crystal
## string is immutable
str = ""
str.object_id #=> 94763530681144_u64
str += "abc"
str.object_id #=> 94494250273768_u64
alias A = Int32 -> Int32
A.new do |x|
return x
end #=> Proc(Int32, Int32)
# crystal
def a(*args : Int32)
args
end
a(1, 2) #=> {1, 2}
a(1) #=> {1}
def b(a, b)
a + b
# crystal
arr = [1, 2, 3, 4, 5]
arr[1..-1] #=> [2, 3, 4, 5]
arr[1...-1] #=> [2, 3, 4]
arr[-2..-1] #=> [4, 5]
(1..2).includes?(2) #=> true
(1...2).includes?(2) #=> false
# 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 ':'