summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFaustino Aguilar <faustinoaq@gmail.com>2018-05-17 11:26:09 +0000
committerGeorg Brandl <georg@python.org>2019-11-28 06:47:50 +0100
commit2f00b79d1b1097bec90f406db68662353594c0b9 (patch)
tree4b465b4ca6358f6ae8590ab1bf4006b0b3a8b49b
parent290b7373ec568cf89a8a21e0e95a7918aecdb0e3 (diff)
downloadpygments-git-2f00b79d1b1097bec90f406db68662353594c0b9.tar.gz
Update test.cr to match latest crystal syntax
-rw-r--r--tests/examplefiles/test.cr806
1 files changed, 246 insertions, 560 deletions
diff --git a/tests/examplefiles/test.cr b/tests/examplefiles/test.cr
index 028ff6f3..79fc1711 100644
--- a/tests/examplefiles/test.cr
+++ b/tests/examplefiles/test.cr
@@ -1,6 +1,6 @@
# Examples taken from http://crystal-lang.org/docs/
-# Copyright 2012-2016 Manas Technology Solutions.
-
+# Copyright 2012-2018 Manas Technology Solutions.
+# Updated on May 17th, 2018 by @faustinoaq.
require "http/server"
@@ -12,7 +12,6 @@ end
puts "Listening on http://0.0.0.0:8080"
server.listen
-
module HTTP
class RequestHandler
end
@@ -23,10 +22,9 @@ alias NumericValue = Float32 | Float64 | Int32 | Int64
enum Time::DayOfWeek
end
-
-$global_greeting = "Hello world"
-
class Greeting
+ class_property global_greeting = "Hello world"
+
@@default_greeting = "Hello world"
def initialize(@custom_greeting = nil)
@@ -38,11 +36,9 @@ class Greeting
end
end
-
LUCKY_NUMBERS = [3, 7, 11]
DOCUMENTATION_URL = "http://crystal-lang.org/docs"
-
module Scorecard
class Parser
def parse(score_text)
@@ -57,7 +53,6 @@ module Scorecard
end
end
-
module Money
CURRENCIES = {
"EUR" => 1.0,
@@ -83,7 +78,6 @@ module Money
end
end
-
i = 0
while i < 10
proc = ->(x : Int32) do
@@ -97,7 +91,6 @@ end
Fiber.yield
-
# A buffered channel of capacity 2
channel = Channel(Int32).new(2)
@@ -111,14 +104,11 @@ end
puts channel.receive
end
-
class MyDictionary(K, V)
end
-
-MyBox.new(1) #:: MyBox(Int32)
-MyBox.new("hello") #:: MyBox(String)
-
+MyBox.new(1) # :: MyBox(Int32)
+MyBox.new("hello") # :: MyBox(String)
module Moo(T)
def t
@@ -136,7 +126,6 @@ end
foo = Foo.new(1)
foo.t # Int32
-
class Parent(T)
end
@@ -146,58 +135,41 @@ end
class GenericChild(T) < Parent(T)
end
-
class Person
end
-
a = 1
ptr = pointerof(a)
-ptr[100_000] = 2 # undefined behaviour, probably a segmentation fault
-
+ptr[100_000] = 2 # undefined behaviour, probably a segmentation fault
alias Int32OrString = Int32 | String
-
alias Int32OrNil = Int32?
-
alias Int32OrNil_ = Int32 | ::Nil
-
alias Int32Ptr = Int32*
-
alias Int32Ptr_ = Pointer(Int32)
-
alias Int32_8 = Int32[8]
-
alias Int32_8_ = StaticArray(Int32, 8)
-
alias Int32StringTuple = {Int32, String}
-
alias Int32StringTuple_ = Tuple(Int32, String)
-
alias Int32ToString = Int32 -> String
-
alias Int32ToString_ = Proc(Int32, String)
-
alias ProcThatReturnsInt32 = -> Int32
-
alias Int32AndCharToString = Int32, Char -> String
-
alias ComplexProc = (Int32 -> Int32) -> String
-
def foo(x : Int32)
"instance"
end
@@ -209,7 +181,6 @@ end
foo 1 # "instance"
foo Int32 # "class"
-
class Parent
end
@@ -223,7 +194,6 @@ ary = [] of Parent.class
ary << Child1
ary << Child2
-
# Same as not specifying a restriction, not very useful
def foo(x : _)
end
@@ -232,10 +202,8 @@ end
def foo(x : _, _ -> Int32)
end
-
-#alias SameAsInt32 = typeof(2)
-#alias Int32OrString_ = typeof(1, "a")
-
+# alias SameAsInt32 = typeof(2)
+# alias Int32OrString_ = typeof(1, "a")
class Person
def initialize(name)
@@ -252,15 +220,13 @@ class Person
end
end
-
john = Person.new "John"
peter = Person.new "Peter"
-john.name #=> "John"
-john.age #=> 0
-
-peter.name #=> "Peter"
+john.name # => "John"
+john.age # => 0
+peter.name # => "Peter"
class Person
def self.new(name)
@@ -268,8 +234,7 @@ class Person
instance.initialize(name)
instance
end
- end
-
+end
if a.is_a?(String)
# here a is a String
@@ -279,7 +244,6 @@ if b.is_a?(Number)
# here b is a Number
end
-
a = some_condition ? 1 : "hello"
# a : Int32 | String
@@ -289,15 +253,12 @@ else
# a : String
end
-
if a.is_a?(String) && b.is_a?(Number)
# here a is a String and b is a Number
end
-
a.+(b)
-
struct Vector2
getter x, y
@@ -311,10 +272,7 @@ end
v1 = Vector2.new(1, 2)
v2 = Vector2.new(3, 4)
-v1 + v2 #=> Vector2(@x=4, @y=6)
-
-
-
+v1 + v2 # => Vector2(@x=4, @y=6)
struct Vector2
def -
@@ -323,11 +281,7 @@ struct Vector2
end
v1 = Vector2.new(1, 2)
--v1 #=> Vector2(@x=-1, @y=-2)
-
-
-
-
+-v1 # => Vector2(@x=-1, @y=-2)
class MyArray
def [](index)
@@ -353,22 +307,18 @@ array.[](1) # invokes the first method
array.[](1, 2, 3) # invokes the second method
array.[]=(1, 2) # invokes the third method
-
raise "OH NO!"
raise Exception.new("Some error")
-
class MyException < Exception
end
-
begin
raise MyException.new("OH NO!")
rescue ex : MyException
puts "Rescued MyException: #{ex.message}"
end
-
begin
# ...
rescue ex : MyException | MyOtherException
@@ -379,19 +329,16 @@ ensure
puts "Cleanup..."
end
-
def some_method
something_dangerous
rescue
# execute if an exception is raised
end
-
array = [1, 2, 3]
array[4] # raises because of IndexError
array[4]? # returns nil because of index out of bounds
-
def some_proc(&block : Int32 -> Int32)
block
end
@@ -399,18 +346,16 @@ end
x = 0
proc = ->(i : Int32) { x += i }
proc = some_proc(&proc)
-proc.call(1) #=> 1
-proc.call(10) #=> 11
-x #=> 11
-
+proc.call(1) # => 1
+proc.call(10) # => 11
+x # => 11
def add(x, y)
x + y
end
adder = ->add(Int32, Int32)
-adder.call(1, 2) #=> 3
-
+adder.call(1, 2) # => 3
module Curses
class Window
@@ -419,7 +364,6 @@ end
Curses::Window.new
-
module ItemsSize
def size
items.size
@@ -435,8 +379,7 @@ class Items
end
items = Items.new
-items.size #=> 3
-
+items.size # => 3
module Base64
extend self
@@ -450,8 +393,7 @@ module Base64
end
end
-Base64.encode64 "hello" #=> "aGVsbG8="
-
+Base64.encode64 "hello" # => "aGVsbG8="
if some_condition
a = 1
@@ -459,13 +401,11 @@ else
a = "hello"
end
-a_as_int = a as Int32
-a_as_int.abs # works, compiler knows that a_as_int is Int32
-
+a_as_int = a.as(Int32)
+a_as_int.abs # works, compiler knows that a_as_int is Int32
ptr = Pointer(Int32).malloc(1)
-ptr as Int8* #:: Pointer(Int8)
-
+ptr.as(Int8*) # :: Pointer(Int8)
array = [1, 2, 3]
@@ -475,24 +415,21 @@ ptr = Pointer(Void).new(array.object_id)
# Now we cast that pointer to the same type, and
# we should get the same value
-array2 = ptr as Array(Int32)
-array2.same?(array) #=> true
-
+array2 = ptr.as(Array(Int32))
+array2.same?(array) # => true
a = 1
-b = a as Int32 | Float64
-b #:: Int32 | Float64
-
+b = a.as(Int32 | Float64)
+b # :: Int32 | Float64
ary = [1, 2, 3]
# We want to create an array 1, 2, 3 of Int32 | Float64
-ary2 = ary.map { |x| x as Int32 | Float64 }
+ary2 = ary.map { |x| x.as(Int32 | Float64) }
-ary2 #:: Array(Int32 | Float64)
+ary2 # :: Array(Int32 | Float64)
ary2 << 1.5 # OK
-
class Person
def initialize(@name)
end
@@ -505,23 +442,19 @@ end
a = [] of Person
x = a.map { |f| f.name } # Error: can't infer block return type
-
a = [] of Person
-x = a.map { |f| f.name as String } # OK
-
+x = a.map { |f| f.name.as(String) } # OK
Person.new "John"
a = [] of Person
x = a.map { |f| f.name } # OK
-
loop do
do_something
break if some_condition
end
-
class Point
def initialize(@x, @y)
end
@@ -530,8 +463,7 @@ end
Point.new 1, 2
# 2 x Int32 = 2 x 4 = 8
-instance_sizeof(Point) #=> 12
-
+instance_sizeof(Point) # => 12
a = 1
while a < 5
@@ -541,8 +473,8 @@ while a < 5
end
puts a
end
-# The above prints the numbers 2, 4 and 5
+# The above prints the numbers 2, 4 and 5
lib C
# In C: double cos(double x)
@@ -557,14 +489,13 @@ lib C
fun printf(format : UInt8*, ...) : Int32
end
-C.cos(1.5) #=> 0.0707372
+C.cos(1.5) # => 0.0707372
C.srand(1_u32)
a = 1
b = 2
C.printf "%d + %d = %d\n", a, b, a + b
-
lib LibSDL
fun init = SDL_Init(flags : UInt32) : Int32
end
@@ -581,53 +512,44 @@ end
lib LibPCRE
end
-
lib C
- ifdef x86_64
+ {% if flag?(:x86_64) %}
alias SizeT = UInt64
- else
+ {% else %}
alias SizeT = UInt32
- end
+ {% end %}
fun memcmp(p1 : Void*, p2 : Void*, size : C::SizeT) : Int32
end
-
lib X
enum SomeEnum
- Ten = 10
- Twenty = 10 * 2
+ Ten = 10
+ Twenty = 10 * 2
ThirtyTwo = 1 << 5
end
end
-
lib X
enum SomeEnum
A = 1_u32
end
end
-
-X::SomeEnum::Zero #=> 0_i8
-X::SomeEnum::Two #=> 2_i8
-
+X::SomeEnum::Zero # => 0_i8
+X::SomeEnum::Two # => 2_i8
lib X
fun callback(f : Int32 -> Int32)
end
-
f = ->(x : Int32) { x + 1 }
X.callback(f)
-
X.callback ->(x) { x + 1 }
-
X.callback nil
-
lib LibFoo
fun store_callback(callback : ->)
fun execute_callback
@@ -636,7 +558,6 @@ end
LibFoo.store_callback ->{ raise "OH NO!" }
LibFoo.execute_callback
-
lib LibFoo
fun store_callback(callback : ->)
@@ -644,14 +565,12 @@ lib LibFoo
fun execute_callback
end
-
@[Link("pcre")]
lib PCRE
INFO_CAPTURECOUNT = 2
end
-PCRE::INFO_CAPTURECOUNT #=> 2
-
+PCRE::INFO_CAPTURECOUNT # => 2
lib U
# In C:
@@ -666,19 +585,15 @@ lib U
end
end
-
value = U::IntOrFloat.new
-
value = uninitialized U::IntOrFlaot
-value.some_int #=> some garbage value
-
+value.some_int # => some garbage value
value = U::IntOrFloat.new
value.some_int = 1
-value.some_int #=> 1
-value.some_float #=> 4.94066e-324
-
+value.some_int # => 1
+value.some_float # => 4.94066e-324
def change_it(value)
value.some_int = 1
@@ -686,8 +601,7 @@ end
value = U::IntOrFloat.new
change_it value
-value.some_int #=> 0
-
+value.some_int # => 0
lib C
# In C:
@@ -698,11 +612,10 @@ lib C
# };
struct TimeZone
minutes_west : Int32
- dst_time : Int32
+ dst_time : Int32
end
end
-
lib C
# This is a forward declaration
struct Node
@@ -713,23 +626,18 @@ lib C
end
end
-
tz = C::TimeZone.new
-
tz = uninitialized C::TimeZone
-tz.minutes_west #=> some garbage value
-
+tz.minutes_west # => some garbage value
tz = C::TimeZone.new
tz.minutes_west = 1
-tz.minutes_west #=> 1
-
+tz.minutes_west # => 1
tz = C::TimeZone.new minutes_west: 1, dst_time: 2
-tz.minutes_west #=> 1
-tz.dst_time #=> 2
-
+tz.minutes_west # => 1
+tz.dst_time # => 2
def change_it(tz)
tz.minutes_west = 1
@@ -737,73 +645,71 @@ end
tz = C::TimeZone.new
change_it tz
-tz.minutes_west #=> 0
-
+tz.minutes_west # => 0
lib C
$errno : Int32
end
-
-C.errno #=> some value
+C.errno # => some value
C.errno = 0
-C.errno #=> 0
-
+C.errno # => 0
lib C
@[ThreadLocal]
$errno : Int32
end
-
lib C
fun waitpid(pid : Int32, status_ptr : Int32*, options : Int32) : Int32
end
-
status_ptr = uninitialized Int32
C.waitpid(pid, pointerof(status_ptr), options)
-
C.waitpid(pid, out status_ptr, options)
-
lib X
type CInt = Int32
end
-
-ifdef x86_64
+{% if flag?(:x86_64) %}
# some specific code for 64 bits platforms
-else
+{% else %}
# some specific code for non-64 bits platforms
-end
-
+{% end %}
-ifdef linux && x86_64
+{% if flag?(:linux) && flag?(:x86_64) %}
# some specific code for linux 64 bits
-end
-
+{% end %}
lib C
- ifdef linux
+ {% if flag?(:linux) && flag?(:x86_64) %}
struct SomeStruct
some_field : Int32
end
- else
+ {% else %}
struct SomeStruct
some_field : Int64
end
- end
+ {% end %}
end
-
# Assigns to a local variable
local = 1
-# Assigns to a global variable
-$global = 4
+# Assigns to a global property
+class Global
+ class_property global1 = 1
+ class_getter global2 = 2
+ class_setter global3 = 3
+
+ # Fails on nil
+ class_property! global4 = 4
+ class_getter! global5 = 5
+ class_setter! global6 = 6
+end
class Testing
# Assigns to an instance variable
@@ -813,8 +719,7 @@ class Testing
@@class = 3
end
-
-local += 1 # same as: local = local + 1
+local += 1 # same as: local = local + 1
# The above is valid with these operators:
# +, -, *, /, %, |, &, ^, **, <<, >>
@@ -822,7 +727,6 @@ local += 1 # same as: local = local + 1
local ||= 1 # same as: local || (local = 1)
local &&= 1 # same as: local && (local = 1)
-
# A setter
person.name=("John")
@@ -841,30 +745,26 @@ objects.[](2, 3)
# The above can be written as:
objects[2, 3]
-
-person.age += 1 # same as: person.age = person.age + 1
+person.age += 1 # same as: person.age = person.age + 1
person.name ||= "John" # same as: person.name || (person.name = "John")
person.name &&= "John" # same as: person.name && (person.name = "John")
-objects[1] += 2 # same as: objects[1] = objects[1] + 2
-
-objects[1] ||= 2 # same as: objects[1]? || (objects[1] = 2)
-objects[1] &&= 2 # same as: objects[1]? && (objects[1] = 2)
+objects[1] += 2 # same as: objects[1] = objects[1] + 2
+objects[1] ||= 2 # same as: objects[1]? || (objects[1] = 2)
+objects[1] &&= 2 # same as: objects[1]? && (objects[1] = 2)
alias PInt32 = Pointer(Int32)
ptr = PInt32.malloc(1) # : Pointer(Int32)
-
alias RecArray = Array(Int32) | Array(RecArray)
ary = [] of RecArray
ary.push [1, 2, 3]
ary.push ary
-ary #=> [[1, 2, 3], [...]]
-
+ary # => [[1, 2, 3], [...]]
module Json
alias Type = Nil |
@@ -876,12 +776,11 @@ module Json
Hash(String, Type)
end
-
a = 1
if a > 0
a = 10
end
-a #=> 10
+a # => 10
b = 1
if b > 2
@@ -889,8 +788,7 @@ if b > 2
else
b = 20
end
-b #=> 20
-
+b # => 20
if some_condition
do_something
@@ -900,7 +798,6 @@ else
do_that
end
-
a = 1
if some_condition
a = "hello"
@@ -927,7 +824,6 @@ if some_condition
end
# d : Int32 | Nil
-
a = 1
if some_condition
a = "hello"
@@ -936,7 +832,6 @@ if some_condition
end
# a : String | Int32
-
if some_condition
e = 1
else
@@ -946,46 +841,38 @@ else
end
# e : Int32
-
enum Color : UInt8
- Red # 0
- Green # 1
- Blue = 5 # overwritten to 5
- Yellow # 6 (5 + 1)
+ Red # 0
+ Green # 1
+ Blue = 5 # overwritten to 5
+ Yellow # 6 (5 + 1)
def red?
self == Color::Red
end
end
-Color::Red.value #:: UInt8
-
+Color::Red.value # :: UInt8
@[Flags]
enum IOMode
- Read # 1
- Write # 2
+ Read # 1
+ Write # 2
Async # 4
end
-
-IOMode::None.value #=> 0
-IOMode::All.value #=> 7
-
+IOMode::None.value # => 0
+IOMode::All.value # => 7
puts(Color::Red) # prints "Red"
puts(IOMode::Write | IOMode::Async) # prints "Write, Async"
+puts Color.new(1) # => prints "Green"
-puts Color.new(1) #=> prints "Green"
-
-
-puts Color.new(10) #=> prints "10"
-
-
-Color::Red.red? #=> true
-Color::Blue.red? #=> false
+puts Color.new(10) # => prints "10"
+Color::Red.red? # => true
+Color::Blue.red? # => false
def paint(color : Color)
case color
@@ -999,7 +886,6 @@ end
paint Color::Red
-
def paint(color : Symbol)
case color
when :red
@@ -1011,17 +897,15 @@ end
paint :red
-
name = "Crystal"
age = 1
-
flower = "Tulip"
# At this point 'flower' is a String
flower = 1
-# At this point 'flower' is an Int32
+# At this point 'flower' is an Int32
class Foo
def finalize
@@ -1035,15 +919,13 @@ loop do
Foo.new
end
-
# Defines a method in the program
def add(x, y)
x + y
end
# Invokes the add method in the program
-add(1, 2) #=> 3
-
+add(1, 2) # => 3
def even?(num)
if num % 2 == 0
@@ -1053,7 +935,6 @@ def even?(num)
return false
end
-
def add(x, y)
x + y
end
@@ -1072,15 +953,14 @@ class Foo
end
end
-
def baz(x, y)
x + y
end
class Foo
def bar
- baz(4, 2) #=> 2
- ::baz(4, 2) #=> 6
+ baz(4, 2) # => 2
+ ::baz(4, 2) # => 6
end
def baz(x, y)
@@ -1088,7 +968,6 @@ class Foo
end
end
-
x = 1
def add(y)
@@ -1097,10 +976,8 @@ end
add(2)
-
add 1, 2 # same as add(1, 2)
-
class Counter
@@instances = 0
@@ -1113,12 +990,11 @@ class Counter
end
end
-Counter.instances #=> 0
+Counter.instances # => 0
Counter.new
Counter.new
Counter.new
-Counter.instances #=> 3
-
+Counter.instances # => 3
class Counter
def self.increment
@@ -1128,7 +1004,6 @@ end
Counter.increment # Error: undefined method '+' for Nil
-
class Parent
@@counter = 0
end
@@ -1139,8 +1014,7 @@ class Child < Parent
end
end
-Child.counter #=> nil
-
+Child.counter # => nil
unless some_condition
then_expression
@@ -1151,17 +1025,13 @@ end
# Can also be written as a suffix
close_door unless door_closed?
-
a = 1
-b = typeof(a) #=> Int32
-
-
-typeof(1, "a", 'a') #=> (Int32 | String | Char)
+b = typeof(a) # => Int32
+typeof(1, "a", 'a') # => (Int32 | String | Char)
hash = {} of Int32 => String
-another_hash = typeof(hash).new #:: Hash(Int32, String)
-
+another_hash = typeof(hash).new # :: Hash(Int32, String)
class Array
def self.elem_type(typ)
@@ -1175,19 +1045,16 @@ end
nest = [1, ["b", [:c, ['d']]]]
flat = Array(typeof(Array.elem_type(nest))).new
-typeof(nest) #=> Array(Int32 | Array(String | Array(Symbol | Array(Char))))
-typeof(flat) #=> Array(String | Int32 | Symbol | Char)
-
+typeof(nest) # => Array(Int32 | Array(String | Array(Symbol | Array(Char))))
+typeof(flat) # => Array(String | Int32 | Symbol | Char)
a = 2 if some_condition
-
x = 0
proc = ->{ x += 1; x }
-proc.call #=> 1
-proc.call #=> 2
-x #=> 2
-
+proc.call # => 1
+proc.call # => 2
+x # => 2
def counter
x = 0
@@ -1195,9 +1062,8 @@ def counter
end
proc = counter
-proc.call #=> 1
-proc.call #=> 2
-
+proc.call # => 1
+proc.call # => 2
def foo
yield
@@ -1209,7 +1075,6 @@ foo do
end
x # : Int32 | String
-
x = 1
foo do
x = "hello"
@@ -1219,7 +1084,6 @@ x # : Int32 | String
x = 'a'
x # : Char
-
def capture(&block)
block
end
@@ -1230,7 +1094,6 @@ capture { x = "hello" }
x = 'a'
x # : Int32 | String | Char
-
def capture(&block)
block
end
@@ -1241,7 +1104,6 @@ x = 1
x = 'a'
x # : Int32 | String | Char
-
abstract class Animal
# Makes this animal talk
abstract def talk
@@ -1269,9 +1131,7 @@ end
john = Person.new "John", Dog.new
peter = Person.new "Peter", Cat.new
-
-john.pet.talk #=> "Woof!"
-
+john.pet.talk # => "Woof!"
a = 1 > 2 ? 3 : 4
@@ -1282,21 +1142,18 @@ a = if 1 > 2
4
end
-
def some_method : String
"hello"
end
-
PI = 3.14
module Earth
RADIUS = 6_371_000
end
-PI #=> 3.14
-Earth::RADIUS #=> 6_371_000
-
+PI # => 3.14
+Earth::RADIUS # => 6_371_000
TEN = begin
a = 0
@@ -1306,8 +1163,7 @@ TEN = begin
a
end
-TEN #=> 10
-
+TEN # => 10
class Person
getter name
@@ -1318,19 +1174,16 @@ class Person
end
john = Person.new "John"
-john.name #=> "John"
-john.name.size #=> 4
-
+john.name # => "John"
+john.name.size # => 4
one = Person.new 1
-one.name #=> 1
-one.name + 2 #=> 3
-
+one.name # => 1
+one.name + 2 # => 3
john = Person.new "John"
one = Person.new 1
-
john = Person.new "John"
one = Person.new 1
@@ -1340,12 +1193,10 @@ john.name.size
# Error: no overload matches 'String#+' with types Int32
john.name + 3
-
john = Person.new "John"
john.name.size
one = Person.new 1
-
class Person
getter name
@@ -1364,11 +1215,9 @@ end
john = Person.new "John"
john.address = "Argentina"
-
# Error: undefined method 'size' for Nil
john.address.size
-
class Person
@age = 0
@@ -1376,7 +1225,6 @@ class Person
end
end
-
class Person
@age : Int32
@@ -1385,27 +1233,23 @@ class Person
end
end
-
a = if 2 > 1
3
else
4
end
-a #=> 3
-
+a # => 3
if 1 > 2
else
3
end
-
def twice(&block)
yield
yield
end
-
twice() do
puts "Hello!"
end
@@ -1416,7 +1260,6 @@ end
twice { puts "Hello!" }
-
def twice
yield 1
yield 2
@@ -1426,10 +1269,8 @@ twice do |i|
puts "Got #{i}"
end
-
twice { |i| puts "Got #{i}" }
-
def many
yield 1, 2, 3
end
@@ -1440,7 +1281,6 @@ end
# Output: 6
-
def many
yield 1, 2, 3
end
@@ -1451,7 +1291,6 @@ end
# Output: 3
-
def twice
yield
yield
@@ -1461,7 +1300,6 @@ twice do |i|
puts i.inspect
end
-
def some
yield 1, 'a'
yield true, "hello"
@@ -1473,22 +1311,17 @@ some do |first, second|
# second is Char | String | Nil
end
-
method do |argument|
argument.some_method
end
-
method(&.some_method)
-
method &.some_method(arg1, arg2)
-
method &.+(2)
method &.[index]
-
def twice
v1 = yield 1
puts v1
@@ -1501,18 +1334,15 @@ twice do |i|
i + 1
end
-
ary = [1, 2, 3]
-ary.map { |x| x + 1 } #=> [2, 3, 4]
-ary.select { |x| x % 2 == 1 } #=> [1, 3]
-
+ary.map { |x| x + 1 } # => [2, 3, 4]
+ary.select { |x| x % 2 == 1 } # => [1, 3]
def transform(value)
yield value
end
-transform(1) { |x| x + 1 } #=> 2
-
+transform(1) { |x| x + 1 } # => 2
def thrice
puts "Before 1"
@@ -1530,15 +1360,13 @@ thrice do |i|
end
end
-
def twice
yield 1
yield 2
end
-twice { |i| i + 1 } #=> 3
-twice { |i| break "hello" } #=> "hello"
-
+twice { |i| i + 1 } # => 3
+twice { |i| break "hello" } # => "hello"
value = twice do |i|
if i == 1
@@ -1546,16 +1374,13 @@ value = twice do |i|
end
i + 1
end
-value #:: Int32 | String
-
+value # :: Int32 | String
values = twice { break 1, 2 }
-values #=> {1, 2}
-
+values # => {1, 2}
value = twice { break }
-value #=> nil
-
+value # => nil
def twice
yield 1
@@ -1571,8 +1396,6 @@ twice do |i|
puts "Got #{i}"
end
-
-
def twice
v1 = yield 1
puts v1
@@ -1593,7 +1416,6 @@ end
# 10
# 3
-
class Foo
def one
1
@@ -1615,7 +1437,6 @@ end
Foo.new.yield_with_self { one } # => 1
Foo.new.yield_normally { one } # => "one"
-
def twice
yield 1
yield 2
@@ -1625,18 +1446,15 @@ twice do |i|
puts "Got: #{i}"
end
-
i = 1
puts "Got: #{i}"
i = 2
puts "Got: #{i}"
-
3.times do |i|
puts i
end
-
struct Int
def times
i = 0
@@ -1647,14 +1465,12 @@ struct Int
end
end
-
i = 0
while i < 3
puts i
i += 1
end
-
class Person
def initialize(@name)
end
@@ -1670,7 +1486,6 @@ end
employee = Employee.new "John"
employee.greet # "Hi, I'm John"
-
class Person
def initialize(@name)
end
@@ -1682,9 +1497,8 @@ class Employee < Person
end
Employee.new "John", "Acme" # OK
-Employee.new "Peter" # Error: wrong number of arguments
- # for 'Employee:Class#new' (1 for 2)
-
+Employee.new "Peter" # Error: wrong number of arguments
+# for 'Employee:Class#new' (1 for 2)
class Person
def greet(msg)
@@ -1704,7 +1518,6 @@ p.greet "everyone" # "Hi, everyone"
e = Employee.new
e.greet "everyone" # "Hello, everyone"
-
class Person
def greet(msg)
puts "Hi, #{msg}"
@@ -1722,10 +1535,9 @@ e.greet "everyone" # "Hi, everyone"
e.greet 1 # "Hi, this is a number: 1"
-
class Person
def greet(msg)
- puts "Hello, "#{msg}"
+ puts "Hello, " # {msg}"
end
end
@@ -1736,14 +1548,12 @@ class Employee < Person
end
end
-
def int_to_int(&block : Int32 -> Int32)
block
end
proc = int_to_int { |x| x + 1 }
-proc.call(1) #=> 2
-
+proc.call(1) # => 2
class Model
def on_save(&block)
@@ -1761,7 +1571,6 @@ model = Model.new
model.on_save { puts "Saved!" }
model.save # prints "Saved!"
-
def some_proc(&block : Int32 ->)
block
end
@@ -1769,7 +1578,6 @@ end
proc = some_proc { |x| x + 1 }
proc.call(1) # void
-
def some_proc(&block : Int32 -> _)
block
end
@@ -1780,15 +1588,13 @@ proc.call(1) # 2
proc = some_proc { |x| x.to_s }
proc.call(1) # "1"
-
macro update_x
x = 1
end
x = 0
update_x
-x #=> 1
-
+x # => 1
macro dont_update_x
%x = 1
@@ -1797,8 +1603,7 @@ end
x = 0
dont_update_x # outputs 1
-x #=> 0
-
+x # => 0
macro fresh_vars_sample(*names)
# First declare vars
@@ -1823,10 +1628,11 @@ fresh_vars_sample a, b, c
# __temp_256: 1
# __temp_257: 2
-
class Object
- macro def instance_vars_names : Array(String)
- {{ @type.instance_vars.map &.name.stringify }}
+ macro instance_vars_names
+ def instance_vars_names : Array(String)
+ {{ @type.instance_vars.map &.name.stringify }}
+ end
end
end
@@ -1836,22 +1642,22 @@ class Person
end
person = Person.new "John", 30
-person.instance_vars_names #=> ["name", "age"]
-
+person.instance_vars_names # => ["name", "age"]
class Object
- macro def has_instance_var?(name) : Bool
- # We cannot access name inside the macro expansion here,
- # instead we need to use the macro language to construct an array
- # and do the inclusion check at runtime.
- {{ @type.instance_vars.map &.name.stringify }}.includes? name
+ macro has_instance_var?(name)
+ def has_instance_var?(name) : Bool
+ # We cannot access name inside the macro expansion here,
+ # instead we need to use the macro language to construct an array
+ # and do the inclusion check at runtime.
+ {{ @type.instance_vars.map &.name.stringify }}.includes? name
+ end
end
end
person = Person.new "John", 30
-person.has_instance_var?("name") #=> true
-person.has_instance_var?("birthday") #=> false
-
+person.has_instance_var?("name") # => true
+person.has_instance_var?("birthday") # => false
class Parent
macro inherited
@@ -1864,8 +1670,7 @@ end
class Child < Parent
end
-Child.new.child #=> 1
-
+Child.new.child # => 1
macro method_missing(name, args, block)
print "Got ", {{name.id.stringify}}, " with ", {{args.size}}, " arguments", '\n'
@@ -1874,19 +1679,15 @@ end
foo # Prints: Got foo with 0 arguments
bar 'a', 'b' # Prints: Got bar with 2 arguments
-
-sizeof(Int32) #=> 4
-sizeof(Int64) #=> 8
-
+sizeof(Int32) # => 4
+sizeof(Int64) # => 8
# On a 64 bits machine
-sizeof(Pointer(Int32)) #=> 8
-sizeof(String) #=> 8
-
+sizeof(Pointer(Int32)) # => 8
+sizeof(String) # => 8
a = 1
-sizeof(typeof(a)) #=> 4
-
+sizeof(typeof(a)) # => 4
class Foo
macro emphasize(value)
@@ -1898,8 +1699,7 @@ class Foo
end
end
-Foo.new.yield_with_self { emphasize(10) } #=> "***10***"
-
+Foo.new.yield_with_self { emphasize(10) } # => "***10***"
# This generates:
#
@@ -1908,7 +1708,6 @@ Foo.new.yield_with_self { emphasize(10) } #=> "***10***"
# end
define_method :foo, 1
-
macro define_method(name, content)
def {{name.id}}
{{content}}
@@ -1922,7 +1721,6 @@ end
# end
define_method :foo, 1
-
macro define_method(name, content)
def {{name}}
{% if content == 1 %}
@@ -1936,15 +1734,13 @@ end
define_method foo, 1
define_method bar, 2
-foo #=> one
-bar #=> 2
-
+foo # => one
+bar # => 2
{% if env("TEST") %}
puts "We are in test mode"
{% end %}
-
macro define_dummy_methods(names)
{% for name, index in names %}
def {{name.id}}
@@ -1955,10 +1751,9 @@ end
define_dummy_methods [foo, bar, baz]
-foo #=> 0
-bar #=> 1
-baz #=> 2
-
+foo # => 0
+bar # => 1
+baz # => 2
macro define_dummy_methods(hash)
{% for key, value in hash %}
@@ -1967,10 +1762,10 @@ macro define_dummy_methods(hash)
end
{% end %}
end
-define_dummy_methods({foo: 10, bar: 20})
-foo #=> 10
-bar #=> 20
+define_dummy_methods({foo: 10, bar: 20})
+foo # => 10
+bar # => 20
{% for name, index in ["foo", "bar", "baz"] %}
def {{name.id}}
@@ -1978,10 +1773,9 @@ bar #=> 20
end
{% end %}
-foo #=> 0
-bar #=> 1
-baz #=> 2
-
+foo # => 0
+bar # => 1
+baz # => 2
macro define_dummy_methods(*names)
{% for name, index in names %}
@@ -1993,10 +1787,9 @@ end
define_dummy_methods foo, bar, baz
-foo #=> 0
-bar #=> 1
-baz #=> 2
-
+foo # => 0
+bar # => 1
+baz # => 2
macro println(*values)
print {{*values}}, '\n'
@@ -2004,14 +1797,12 @@ end
println 1, 2, 3 # outputs 123\n
-
VALUES = [1, 2, 3]
{% for value in VALUES %}
puts {{value}}
{% end %}
-
until some_condition
do_this
end
@@ -2021,7 +1812,6 @@ while !some_condition
do_this
end
-
a = some_condition ? nil : 3
# a is Int32 or Nil
@@ -2031,22 +1821,18 @@ if a
a.abs
end
-
if a = some_expression
# here a is not nil
end
-
if a && b
# here both a and b are guaranteed not to be Nil
end
-
if @a
# here @a can be nil
end
-
# First option: assign it to a variable
if a = @a
# here a can't be nil
@@ -2057,13 +1843,11 @@ end
# here a can't be nil
end
-
if method # first call to a method that can return Int32 or Nil
- # here we know that the first call did not return Nil
- method # second call can still return Int32 or Nil
+ # here we know that the first call did not return Nil
+ method # second call can still return Int32 or Nil
end
-
class Person
def become_older(by = 1)
@age += by
@@ -2071,27 +1855,24 @@ class Person
end
john = Person.new "John"
-john.age #=> 0
+john.age # => 0
john.become_older
-john.age #=> 1
+john.age # => 1
john.become_older 2
-john.age #=> 3
-
+john.age # => 3
john.become_older by: 5
-
def some_method(x, y = 1, z = 2, w = 3)
# do something...
end
-some_method 10 # x = 10, y = 1, z = 2, w = 3
-some_method 10, z: 10 # x = 10, y = 1, z = 10, w = 3
+some_method 10 # x = 10, y = 1, z = 2, w = 3
+some_method 10, z: 10 # x = 10, y = 1, z = 10, w = 3
some_method 10, w: 1, y: 2, z: 3 # x = 10, y = 2, z = 3, w = 1
-
case exp
when value1, value2
do_something
@@ -2101,7 +1882,6 @@ else
do_another_thing
end
-
case var
when String
# var : String
@@ -2114,7 +1894,6 @@ else
do_another_thing
end
-
case num
when .even?
do_something
@@ -2122,7 +1901,6 @@ when .odd?
do_something_else
end
-
case
when cond1, cond2
do_something
@@ -2130,29 +1908,22 @@ when cond3
do_something_else
end
-
a = 1
-a.responds_to?(:abs) #=> true
-a.responds_to?(:size) #=> false
-
+a.responds_to?(:abs) # => true
+a.responds_to?(:size) # => false
foo_or_bar = /foo|bar/
-heeello = /h(e+)llo/
-integer = /\d+/
-
+heeello = /h(e+)llo/
+integer = /\d+/
r = /foo/imx
-
slash = /\//
-
r = %r(regex with slash: /)
-
"hello world"
-
"\"" # double quote
"\\" # backslash
"\e" # escape
@@ -2162,34 +1933,27 @@ r = %r(regex with slash: /)
"\t" # tab
"\v" # vertical tab
-
"\101" # == "A"
"\123" # == "S"
"\12" # == "\n"
"\1" # string with one character with code point 1
-
"\u0041" # == "A"
-
"\u{41}" # == "A"
-"\u{1F52E}" # == "🔮"
-
+"\u{1F52E}" # == "🔮"
"hello
world" # same as "hello\n world"
-
"hello " \
"world, " \
"no newlines" # same as "hello world, no newlines"
-
"hello \
world, \
no newlines" # same as "hello world, no newlines"
-
# Supports double quotes and nested parenthesis
%(hello ("world")) # same as "hello (\"world\")"
@@ -2202,14 +1966,12 @@ r = %r(regex with slash: /)
# Supports double quotes and nested angles
%<hello <"world">> # same as "hello <\"world\">"
-
<<-XML
<parent>
<child />
</parent>
XML
-
# Same as "Hello\n world"
<<-STRING
Hello
@@ -2222,33 +1984,28 @@ XML
world
STRING
-
a = 1
b = 2
-"sum = #{a + b}" # "sum = 3"
-
+"sum = #{a + b}" # "sum = 3"
-1.0 # Float64
-1.0_f32 # Float32
-1_f32 # Float32
+1.0 # Float64
+1.0_f32 # Float32
+1_f32 # Float32
-1e10 # Float64
-1.5e10 # Float64
-1.5e-7 # Float64
-
-+1.3 # Float64
--0.5 # Float64
+1e10 # Float64
+1.5e10 # Float64
+1.5e-7 # Float64
++1.3 # Float64
+-0.5 # Float64
1_000_000.111_111 # better than 1000000.111111
-
'a'
'z'
'0'
'_'
-'あ'
-
+"あ"
'\'' # single quote
'\\' # backslash
@@ -2259,48 +2016,36 @@ b = 2
'\t' # tab
'\v' # vertical tab
-
-'\101' # == 'A'
-'\123' # == 'S'
-'\12' # == '\n'
-'\1' # code point 1
-
+"\101" # == 'A'
+"\123" # == 'S'
+"\12" # == '\n'
+"\1" # code point 1
'\u0041' # == 'A'
-
'\u{41}' # == 'A'
-'\u{1F52E}' # == '🔮'
-
-
-{1 => 2, 3 => 4} # Hash(Int32, Int32)
-{1 => 2, 'a' => 3} # Hash(Int32 | Char, Int32)
+'\u{1F52E}' # == '🔮'
+{1 => 2, 3 => 4} # Hash(Int32, Int32)
+{1 => 2, 'a' => 3} # Hash(Int32 | Char, Int32)
{} of Int32 => Int32 # same as Hash(Int32, Int32).new
-
{key1: 'a', key2: 'b'} # Hash(Symbol, Char)
-
{"key1": 'a', "key2": 'b'} # Hash(String, Char)
-
-MyType{"foo": "bar"}
-
+MyType{"foo" => "bar"}
tmp = MyType.new
tmp["foo"] = "bar"
tmp
-
tmp = MyType(typeof("foo"), typeof("bar")).new
tmp["foo"] = "bar"
tmp
-
-MyType(String, String) {"foo": "bar"}
-
+MyType(String, String){"foo" => "bar"}
:hello
:good_bye
@@ -2340,11 +2085,9 @@ MyType(String, String) {"foo": "bar"}
:<=>
:===
-
x..y # an inclusive range, in mathematics: [x, y]
x...y # an exclusive range, in mathematics: [x, y)
-
# A proc without arguments
->{ 1 } # Proc(Int32)
@@ -2354,121 +2097,98 @@ x...y # an exclusive range, in mathematics: [x, y)
# A proc with two arguments:
->(x : Int32, y : Int32) { x + y } # Proc(Int32, Int32, Int32)
-
Proc(Int32, String).new { |x| x.to_s } # Proc(Int32, String)
-
proc = ->(x : Int32, y : Int32) { x + y }
-proc.call(1, 2) #=> 3
-
+proc.call(1, 2) # => 3
def one
1
end
proc = ->one
-proc.call #=> 1
-
+proc.call # => 1
def plus_one(x)
x + 1
end
proc = ->plus_one(Int32)
-proc.call(41) #=> 42
-
+proc.call(41) # => 42
str = "hello"
proc = ->str.count(Char)
-proc.call('e') #=> 1
-proc.call('l') #=> 2
-
+proc.call('e') # => 1
+proc.call('l') # => 2
tuple = {1, "hello", 'x'} # Tuple(Int32, String, Char)
-tuple[0] #=> 1 (Int32)
-tuple[1] #=> "hello" (String)
-tuple[2] #=> 'x' (Char)
-
+tuple[0] # => 1 (Int32)
+tuple[1] # => "hello" (String)
+tuple[2] # => 'x' (Char)
[1, 2, 3] # Array(Int32)
[1, "hello", 'x'] # Array(Int32 | String | Char)
-
[] of Int32 # same as Array(Int32).new
-
%w(one two three) # ["one", "two", "three"]
-
%i(one two three) # [:one, :two, :three]
-
MyType{1, 2, 3}
-
tmp = MyType.new
tmp << 1
tmp << 2
tmp << 3
tmp
-
tmp = MyType(typeof(1, 2, 3)).new
tmp << 1
tmp << 2
tmp << 3
tmp
-
-MyType(Int32 | String) {1, 2, "foo"}
-
+MyType(Int32 | String){1, 2, "foo"}
nil
+1 # Int32
-1 # Int32
-
-1_i8 # Int8
-1_i16 # Int16
-1_i32 # Int32
-1_i64 # Int64
+1_i8 # Int8
+1_i16 # Int16
+1_i32 # Int32
+1_i64 # Int64
-1_u8 # UInt8
-1_u16 # UInt16
-1_u32 # UInt32
-1_u64 # UInt64
+1_u8 # UInt8
+1_u16 # UInt16
+1_u32 # UInt32
+1_u64 # UInt64
-+10 # Int32
--20 # Int32
++10 # Int32
+-20 # Int32
2147483648 # Int64
9223372036854775808 # UInt64
-
1_000_000 # better than 1000000
-
0b1101 # == 13
-
0o123 # == 83
-
0xFE012D # == 16646445
0xfe012d # == 16646445
-
true # A Bool that is true
false # A Bool that is false
-
a = 1
ptr = pointerof(a)
ptr.value = 2
-a #=> 2
-
+a # => 2
class Point
def initialize(@x, @y)
@@ -2488,8 +2208,7 @@ point = Point.new 1, 2
ptr = point.x_ptr
ptr.value = 10
-point.x #=> 10
-
+point.x # => 10
def add(x : Number, y : Number)
x + y
@@ -2501,14 +2220,12 @@ add 1, 2 # Ok
# Error: no overload matches 'add' with types Bool, Bool
add true, false
-
def add(x, y)
x + y
end
add true, false
-
# A class that has a + method but isn't a Number
class Six
def +(other)
@@ -2532,7 +2249,6 @@ end
# Error: no overload matches 'restricted_add' with types Six, Int32
restricted_add Six.new, 10
-
class Person
def ==(other : self)
other.name == name
@@ -2547,10 +2263,9 @@ john = Person.new "John"
another_john = Person.new "John"
peter = Person.new "Peter"
-john == another_john #=> true
-john == peter #=> false (names differ)
-john == 1 #=> false (because 1 is not a Person)
-
+john == another_john # => true
+john == peter # => false (names differ)
+john == 1 # => false (because 1 is not a Person)
class Person
def self.compare(p1 : self, p2 : self)
@@ -2563,21 +2278,18 @@ peter = Person.new "Peter"
Person.compare(john, peter) # OK
-
def foo(x : Int32)
end
foo 1 # OK
foo "hello" # Error
-
def foo(x : Int32.class)
end
foo Int32 # OK
foo String # Error
-
def foo(x : Int32.class)
puts "Got Int32"
end
@@ -2589,7 +2301,6 @@ end
foo Int32 # prints "Got Int32"
foo String # prints "Got String"
-
def foo(*args : Int32)
end
@@ -2601,35 +2312,30 @@ foo "a", "b", "c" # OK, invokes second overload
foo 1, 2, "hello" # Error
foo() # Error
-
def foo
# This is the empty-tuple case
end
-
def foo(x : T)
T
end
-foo(1) #=> Int32
-foo("hello") #=> String
-
+foo(1) # => Int32
+foo("hello") # => String
def foo(x : Array(T))
T
end
-foo([1, 2]) #=> Int32
-foo([1, "a"]) #=> (Int32 | String)
-
+foo([1, 2]) # => Int32
+foo([1, "a"]) # => (Int32 | String)
def foo(x : T.class)
Array(T)
end
-foo(Int32) #=> Array(Int32)
-foo(String) #=> Array(String)
-
+foo(Int32) # => Array(Int32)
+foo(String) # => Array(String)
class Person
# Increases age by one
@@ -2657,50 +2363,44 @@ end
person = Person.new "John"
person.become_older
-person.age #=> 1
+person.age # => 1
person.become_older 5
-person.age #=> 6
+person.age # => 6
person.become_older "12"
-person.age #=> 18
+person.age # => 18
person.become_older do |current_age|
current_age < 20 ? 10 : 30
end
-person.age #=> 28
-
+person.age # => 28
a = 1
-a.is_a?(Int32) #=> true
-a.is_a?(String) #=> false
-a.is_a?(Number) #=> true
-a.is_a?(Int32 | String) #=> true
-
+a.is_a?(Int32) # => true
+a.is_a?(String) # => false
+a.is_a?(Number) # => true
+a.is_a?(Int32 | String) # => true
# One for each thread
@[ThreadLocal]
-$values = [] of Int32
-
+values = [] of Int32
@[AlwaysInline]
def foo
1
end
-
@[NoInline]
def foo
1
end
-
lib LibFoo
@[CallConvention("X86_StdCall")]
fun foo : Int32
end
-
def sum(*elements)
total = 0
elements.each do |value|
@@ -2712,12 +2412,10 @@ end
# elements is Tuple(Int32, Int32, Int32, Float64)
sum 1, 2, 3, 4.5
-
if a.responds_to?(:abs)
# here a's type will be reduced to those responding to the 'abs' method
end
-
a = some_condition ? 1 : "hello"
# a : Int32 | String
@@ -2727,12 +2425,10 @@ else
# here a will be String
end
-
if (a = @a).responds_to?(:abs)
# here a is guaranteed to respond to `abs`
end
-
def capture(&block)
block
end
@@ -2744,9 +2440,6 @@ end
proc = capture { puts "Hello" }
invoke(&proc) # prints "Hello"
-
-
-
def capture(&block)
block
end
@@ -2759,17 +2452,14 @@ end
proc = capture { puts "Hello" }
twice &proc
-
twice &->{ puts "Hello" }
-
def say_hello
puts "Hello"
end
twice &->say_hello
-
def foo
yield 1
end
@@ -2786,7 +2476,6 @@ wrap_foo do |i|
puts i
end
-
def foo
yield 1
end
@@ -2801,12 +2490,10 @@ wrap_foo do |i|
puts i
end
-
foo_forward do |i|
break # error
end
-
a = 2
while (a += 1) < 20
if a == 10
@@ -2814,8 +2501,7 @@ while (a += 1) < 20
break
end
end
-puts a #=> 10
-
+puts a # => 10
class Person
private def say(message)
@@ -2823,7 +2509,7 @@ class Person
end
def say_hello
- say "hello" # OK, no receiver
+ say "hello" # OK, no receiver
self.say "hello" # Error, self is a receiver
other = Person.new "Other"
@@ -2831,14 +2517,12 @@ class Person
end
end
-
class Employee < Person
def say_bye
say "bye" # OK
end
end
-
module Namespace
class Foo
protected def foo
@@ -2856,7 +2540,6 @@ end
Namespace::Bar.new.bar
-
class Person
protected def self.say(message)
puts message
@@ -2867,5 +2550,8 @@ class Person
end
end
-
buffer = uninitialized UInt8[256]
+
+foo = rand(5) > 1 ? 1 : nil
+
+foo.not_nil!.to_i