-
-
Notifications
You must be signed in to change notification settings - Fork 2
lit_syntax_and_semantics
uint256_t edited this page Jul 2, 2016
·
8 revisions
- Comments start with # character
# This is a comment.
# Support only one line.
- Bool has two values
true
false
- There is only 32 bit singed type
314
123456789
- A string literal starts with a double quote ( " ) and end in a double quote ( " )
"Hello world!"
"\n" # new line
"\t" # tab
"\'" # single quote
"\"" # double quote
- An Array is created with only one type.
- An Array cannot have mixed types.
[1, 2, 3] #OK
["hello", "world!"] #OK
[1, "hello"] # NG
- Local variables doesn't start with dollar sign ( $ )
- They are declared when first assignment.
- They are possible to specify types.
n = 10
str = "hello"
i:string = "hello" # specify
ary = [1, 2]
- Global variables start with dollar sign ( $ )
- Under the circumstances, Global variables are not possible to specify types and support only Integer type...
$g = 10
$str = "hello" # impossible..
-
if
evaluates the then branch if its condition istrue
, and evaluates theelse
orelsif
branches.
i = 10
if i < 5
i = 0
elsif i == 5
i = 1
else
i = 2
end
i # => 2
-
while
executes the body while the conditon is true.
i = 0
while i < 10
puts i
i +=1
end
-
for
executes the body.
# First syntax (like C)
for i = 0, i < 10, i += 1
puts i
end
# Second syntax (like Ruby)
for i in 0..9 # 0...10
puts i
# unnecessary increment
end
-
break
breaks out of the loop
i = 0
while 1
if i >= 5
break
end
i +=1
end
- Follow this to define the functions
def f(x) # parentheses may not need
x + 1
end
def f x:string # support overload of function
puts x
end
def f:string # set return type
"hello"
end
def add x:string y:string :string
x + y
end
puts add "hel", "lo" # => hello
-
require
includes the specified file
require "std" # includes all stdlibs
require "Math" # library related to the Mathematics
require "File"
puts File::read "story.txt"
puts Math::sqrt 9.0 # => 3
puts length [1, 2, 3] # => 3
puts length "hello" # => 5