on 448bfaf help sign in
117 lines 2.2 kB history raw
1# An example file to test Crystal syntax highlighting in bat
2my_var : Nil = nil
3my_var_also : Int32 = 42
4my_other_var = 4.0
5another_float = 4.0_f32
6another_float_2 = 4e10
7another_float_3 = -0.5
8big_one = 1_000_000.111_111e-4
9ternary = 1 > 2 : 3 ? 4
10my_symbol = :ThisOne?
11my_other_symbol = :No_That_One!
12plus = :+
13minus = :-
14my_string : String = "this string right here, with an interpolated value of #{my_var_also}"
15my_array : Array(Int32) = [1,2,3,4]
16my_tuple : Tuple(Int32, Int32, Int32, Int32) = {1,2,3,4}
17my_named_tuple : NamedTuple(one: Int32, two: Int32) = {"one": 1, "two": 2}
18my_hash : Hash(String, Int32) = {"one" => 1, "two" => 2}
19my_proc : Proc(Int32, Int32) = ->(x : Int32){ x * x}
20my_other_proc : Proc(String) = ->{ "Wow, neat!" }
21puts my_string
22puts(my_string)
24enum Colors
25 Red
26 Green
27 Blue
28end
30class Greeter
31 @instance_field = Colors::Red
32 @@class_field = Colors::Green
34 def initialize(@name = "world")
35 end
37 def greet
38 puts "Hello, #{@name}"
39 end
41 def render_greeting : String
42 "Hello, #{@name}"
43 end
45 def with_greeting
46 yield render_greeting
47 end
49 def is_color_default?
50 @instance_field == @@class_field
51 end
53 def self.greet_static(name : String) : Unit
54 puts "Hello, #{name}"
55 end
56end
58greeter = Greeter.new("bat")
59greeter.with_greeting do |greeting|
60 puts greeting
61end
63puts <<-EOF
64 this is a heredoc and it has a value in it of #{greeter.render_greeting}!
65EOF
67# This is a command:
68`echo yay!`
69$?.success?
71my_color = Colors::Red
73puts
74 case my_color
75 when Colors::Red, .red?
76 "Red"
77 when Colors::Green, .green?
78 "Green"
79 when Colors::Blue, .blue?
80 "Blue"
81 else
82 "I dunno, man. Chartreuse? Maroon?"
83 end
85class MyGenericClass(T)
86 def initialize(@wrapped_value : T)
87 end
89 def get
90 return @wrapped_value
91 end
92end
95def do_stuff_with_range(r : Range(Int|String))
96 return if r.empty?
97 return unless !(r.empty?)
98 r.each do |item|
99 if /e/.match(item.to_s)
100 puts "#{item} contains the letter e!"
101 elsif item.to_s.empty?
102 break
103 else
104 next # this is unnecessary, but whatever
105 end
106 end
107end
110macro print_range(range)
111 {% for i in range %}
112 puts {{i.id}}
113 {% end %}
114end
116print_range(1..3)
117print_range(1...3)