A language for humans and computers.
nilx = nilx = nil
x = "Hello BrisRuby!"x = nil
# Here 'x' is a Nil
x = "Hello BrisRuby!"
# 'x' is now a Stringif rand > 0.5
x = nil
else
x = "Hello BrisRuby!"
end
x
# ?if rand > 0.5
x = nil
else
x = "Hello BrisRuby!"
end
x
# Here 'x' is a `String?`. This is
# shorthand for `String | Nil`.
#
# This is information that we
# _know_ at compile time."foo".upcase # => "FOO""foo".does_not_exist(Saturday, 2am)
"foo".does_not_existerror in line 1
Error: undefined method 'does_not_exist' for Stringif rand > 0.5
x = "foo"
else
x = 42
end
x.upcaseif rand > 0.5
x = "foo"
else
x = 42
end
x.upcaseError: undefined method 'upcase' for Int32 (compile-time type is (Int32 | String))x = nil
# Nil here
if rand > 0.5
x = "foo"
# All good!
x.upcase
else
x = 42
# Also fine
x * 1729
end
x
# String | Int32class Foo
endclass Foo < Bar
endclass Foo
include Bar
endstruct Foo
endclass Foo
# Accessible to the local instance only
@instance_var : String
# Shared between all `Foo`s
@@class_var : Bool
endclass Foo
def hello
#...
end
endclass Foo
def hello : String
return "Hello world!"
end
endclass Foo
def hello(name : String) : String
return "Hello #{name}!"
end
endclass Foo
def hello(name : String = "world") : String
return "Hello #{name}!"
end
endclass Foo
def hello(name : String = "world", shout : Bool = false) : String
if shout
name = name.upcase
end
return "Hello #{name}!"
end
endclass Foo
def hello(name : String = "world", shout : Bool = false)
if shout
name = name.upcase
end
return "Hello #{name}!"
end
endclass Foo
def hello(name : String = "world", shout : Bool = false)
if shout
name = name.upcase
end
"Hello #{name}!"
end
endclass Foo
def hello(name : String = "world", shout : Bool = false)
name = name.upcase if shout
"Hello #{name}!"
end
endclass Foo
def hello(name = "world", shout = false)
name = name.upcase if shout
"Hello #{name}!"
end
endclass Foo
def hello(name, shout = false)
name = name.upcase if shout
hello name
end
def hello(name = "world")
"Hello #{name}!"
end
enddef twice
yield
yield
end
twice do
puts "Hello!"
enddef twice
yield "world"
yield "humans"
end
twice do |x|
puts "Hello #{x}!"
end3.times do |i|
puts i
end3.times do |i|
puts i
endstruct Int
def times
i = 0
while i < self
yield i
i += 1
end
end
end3.times do |i|
puts i
endstruct Int
def times
i = 0
while i < self
yield i
i += 1
end
end
endi = 0
while i < 3
puts i
i += 1
end3.times do |i|
puts i
endi = 0
while i < 3
puts i
i += 1
endCrystal’s job is to generate LLVM code
—Ary
All in the compiler and standard tools
A welcoming, and friendly community.
crystal-lang.org
PlaceOS.com