Skip to content

Commit

Permalink
format crystal files
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jun 29, 2021
1 parent 7c5c4db commit d8fce67
Show file tree
Hide file tree
Showing 161 changed files with 712 additions and 805 deletions.
8 changes: 4 additions & 4 deletions crystal/examples/add_numbers.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
x = 23
y = 19
z = x + y
puts z # 42
puts "#{x} + #{y} is #{z}" # 23 + 19 is 42
puts z # 42
puts "#{x} + #{y} is #{z}" # 23 + 19 is 42

a = "23"
b = "19"
c = a + b
puts c # 2319
puts "#{a} + #{b} is #{c}" # 23 + 19 is 2319
puts c # 2319
puts "#{a} + #{b} is #{c}" # 23 + 19 is 2319
13 changes: 6 additions & 7 deletions crystal/examples/arrays/array.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
planets = ["Mars", "Jupyter", "Saturn", "Earth"]
p! planets # ["Mars", "Jupyter", "Saturn", "Earth"]
puts typeof(planets) # Array(String)
puts planets.size # 4
p! planets # ["Mars", "Jupyter", "Saturn", "Earth"]
puts typeof(planets) # Array(String)
puts planets.size # 4

integers = [3, 8, -2]
puts typeof(integers) # Array(Int32)
puts typeof(integers) # Array(Int32)

floats = [3.14, 2.1]
puts typeof(floats) # Array(Float64)
puts typeof(floats) # Array(Float64)

mixed = [1, 3.14, "PI"]
puts typeof(mixed) # Array(Float64 | Int32 | String)

puts typeof(mixed) # Array(Float64 | Int32 | String)
6 changes: 3 additions & 3 deletions crystal/examples/arrays/array_iterate.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
planets = ["Mars", "Jupyter", "Saturn", "Earth"]

planets.each {|planet| puts planet}
planets.each { |planet| puts planet }

# enumerate
planets.each_with_index {|planet, idx|
puts "#{idx}: #{planet}"
planets.each_with_index { |planet, idx|
puts "#{idx}: #{planet}"
}
4 changes: 2 additions & 2 deletions crystal/examples/arrays/array_push.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ planets = ["Mars", "Jupyter", "Saturn"]

# append / push / <<
planets << "Pluto"
puts planets # ["Mars", "Jupyter", "Saturn", "Pluto"]
puts planets # ["Mars", "Jupyter", "Saturn", "Pluto"]

planets.push("Venus")
puts planets # ["Mars", "Jupyter", "Saturn", "Pluto", "Venus"]
puts planets # ["Mars", "Jupyter", "Saturn", "Pluto", "Venus"]
8 changes: 4 additions & 4 deletions crystal/examples/arrays/count_digits.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
text = "123456789112777"
count = [0] * 10

text.each_char {|chr|
count[chr.to_i] += 1
text.each_char { |chr|
count[chr.to_i] += 1
}

count.each_with_index {|value, idx|
puts "#{idx} #{value}"
count.each_with_index { |value, idx|
puts "#{idx} #{value}"
}
1 change: 0 additions & 1 deletion crystal/examples/arrays/elements.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ puts planets[-1] # Jupiter
puts planets[0, 3] # ["Mercury", "Venus", "Earth"]
puts planets[0..3] # ["Mercury", "Venus", "Earth", "Mars"]
puts planets[0...3] # ["Mercury", "Venus", "Earth"]

14 changes: 6 additions & 8 deletions crystal/examples/arrays/empty_array.cr
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# empty = [] # Syntax error


empty = [] of Int32
puts empty.size # 0
puts empty.empty? # true
puts empty.size # 0
puts empty.empty? # true

empty << 23
puts empty.size # 1
puts empty.empty? # false
puts empty.size # 1
puts empty.empty? # false

# empty << 3.14 # Error: no overload matches 'Array(Int32)#<<' with type Float64

other = Array(Int32).new
puts typeof(other) # Array(Int32)
puts other.size # 0

puts typeof(other) # Array(Int32)
puts other.size # 0
8 changes: 6 additions & 2 deletions crystal/examples/arrays/filter.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
numbers = [10, 20, 7, 21, 5]
puts numbers
small = numbers.reject do |num| num > 10 end
small = numbers.reject do |num|
num > 10
end
puts small

big = numbers.reject! do |num| num < 10 end
big = numbers.reject! do |num|
num < 10
end
puts big
5 changes: 3 additions & 2 deletions crystal/examples/arrays/map.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

numbers = [2, -3, 4, -5]
puts numbers

doubles = numbers.map do |num| num*2 end
doubles = numbers.map do |num|
num*2
end
puts doubles

triples = numbers.map { |num| num*3 }
Expand Down
1 change: 0 additions & 1 deletion crystal/examples/assign_to_variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ p! typeof(x)
x = 1
p! x
p! typeof(x)

6 changes: 3 additions & 3 deletions crystal/examples/backtick.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
result = `ls -l`
if $?.success?
puts result
puts result
else
puts "Failure"
end
puts "Failure"
end
6 changes: 2 additions & 4 deletions crystal/examples/capture.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

output, error, exit_code = capture("ls", ["-l", "-a"])
if exit_code == 0
puts output
Expand All @@ -7,15 +6,14 @@ else
end
puts exit_code


def capture(cmd, params)
process = Process.new(cmd, params,
output: Process::Redirect::Pipe,
error: Process::Redirect::Pipe,
)
)

output = process.output.gets_to_end
error = process.error.gets_to_end
error = process.error.gets_to_end

res = process.wait

Expand Down
5 changes: 2 additions & 3 deletions crystal/examples/capture2.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
output, error, exit_code = capture("ls", ["-l", "-a"])
if exit_code == 0
puts output
puts output
else
puts error
puts error
end
puts exit_code


def capture(cmd, params)
stdout = IO::Memory.new
stderr = IO::Memory.new
Expand Down
23 changes: 11 additions & 12 deletions crystal/examples/case.cr
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
if ARGV.size != 1
puts "Usage: case.cr DIRECTION"
exit -1
puts "Usage: case.cr DIRECTION"
exit -1
end
direction = ARGV[0]


case direction
when "forward"
puts "go on"
when "bacward"
puts "go back"
when "left", "right"
puts "go #{direction}"
else
puts "We don't know how to go '#{direction}'"
exit 0
when "forward"
puts "go on"
when "bacward"
puts "go back"
when "left", "right"
puts "go #{direction}"
else
puts "We don't know how to go '#{direction}'"
exit 0
end
1 change: 0 additions & 1 deletion crystal/examples/case_when_on_type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ when Int32
puts "int32"
puts x.abs
end

21 changes: 10 additions & 11 deletions crystal/examples/catch_exception.cr
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

filenames = ["README.md", "Other file", "crystal.json"]
filenames.each {|file|
begin
result = process(file)
puts result
rescue err
puts "Rescued! #{err.message}"
puts err.class # File::NotFoundError
end
filenames.each { |file|
begin
result = process(file)
puts result
rescue err
puts "Rescued! #{err.message}"
puts err.class # File::NotFoundError
end
}

def process(filename)
content = File.read(filename)
return content.size
content = File.read(filename)
return content.size
end
Loading

0 comments on commit d8fce67

Please sign in to comment.