-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexample_ruby_scripts.rb
53 lines (46 loc) · 1.18 KB
/
example_ruby_scripts.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class TriangleError < StandardError
end
class PalindromeError < StandardError
end
def is_triangle? args
raise ArgumentError, "There are not 3 arguments" if args.length != 3
array = args
max = array.sort!.pop
raise TriangleError, "This is not a triangle" unless max <= array[0] + array[1]
puts "Congratulations, you have found a triangle!"
end
def another_is_triangle_method args
if args.length != 3
raise ArgumentError, "There are not 3 arguments"
end
array = args
max = array.sort!.pop
unless max <= array[0] + array[1]
raise TriangleError "This is not a triangle"
else
puts "Congratulations, you have found a triangle!"
end
end
def reverse_string string
string.reverse
end
def tedious_reverse_string string
arr, reverse_arr = [], []
arr = string.chars.to_a
arr.length.times do
reverse_arr << arr.pop
end
end
def is_palindrome blob
array = blob.to_s.chars.to_a
if array.length % 2 == 1
array.delete_at((array.length - 1 )/ 2)
end
left = array.slice(0..(array.length/2 - 1))
right = array.slice((array.length/2)..-1)
if left == right.reverse
puts "Found a palindrome!"
else
raise PalindromeError, "Not a Palindrome"
end
end