-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby.rb
More file actions
52 lines (40 loc) · 854 Bytes
/
ruby.rb
File metadata and controls
52 lines (40 loc) · 854 Bytes
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
#!/usr/bin/env ruby
# This is a comment
require 'json'
PI = 3.14159
MAX_USERS = 100
class User
attr_accessor :name, :age
def initialize(name = "Guest", age = 0)
@name = name
@age = age
end
def greet
puts "Hello, #{@name}! You are #{@age} years old."
end
end
def factorial(n)
return 1 if n <= 1
n * factorial(n - 1)
end
def square(x)
x ** 2
end
users = [
User.new("Alice", 25),
User.new("Bob", 30)
]
users.each_with_index do |user, index|
puts "User ##{index + 1}:"
user.greet
end
begin
puts "Factorial of 5 is #{factorial(5)}"
puts "Square of 9 is #{square(9)}"
result = JSON.parse('{"status":"ok"}')
puts "Parsed JSON: #{result['status']}"
rescue StandardError => e
puts "An error occurred: #{e.message}"
ensure
puts "Cleanup complete."
end