-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby_functions_practice.rb
143 lines (119 loc) · 2.67 KB
/
ruby_functions_practice.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# require 'date'
def return_10()
return 10
end
def add(num1,num2)
return num1+num2
end
def subtract(num1,num2)
return num1-num2
end
def multiply(num1,num2)
return num1*num2
end
def divide(num1,num2)
return num1 / num2
end
def length_of_string(string)
return string.length
end
def join_string(string1,string2)
return string1+string2
end
def add_string_as_number(string1, string2)
return string1.to_i + string2.to_i
end
def number_to_full_month_name(number)
case number
when 1
month = "January"
when 2
month = "February"
when 3
month = "March"
when 4
month = "April"
when 5
month = "May"
when 6
month = "June"
when 7
month = "July"
when 8
month = "August"
when 9
month = "September"
when 10
month = "October"
when 11
month = "November"
when 12
month = "December"
end
return month
end
def number_to_short_month_name(month_number)
month = number_to_full_month_name(month_number)
substring = month[0,3]
return substring
end
def volume_of_cube(edge)
volume = edge**3
return volume
end
def volume_of_sphere(radius)
pi = 3.14159262
volume = (4 * pi * (radius.to_f**3.0))/3
return volume.round(1)
end
def days_until_christmas()
leap = Date.leap?(Time.new.year)
leap_next = Date.leap?(Time.new.year+1)
if leap
christmas_yday = 360
elsif !leap
christmas_yday = 359
end
today = Time.new.yday
if today > christmas_yday
if leap_next
christmas_yday += 366
elsif !leap_next
christmas_yday += 365
end
end
days_til_xmas = christmas_yday - today - 1 #-1 because current day doesn't count - only counting whole days aka "nights" til xmas
return days_til_xmas
end
def age_of_person(day_of_birth, month_of_birth, year_of_birth)
#basic version with no month/day awareness:
# age = Time.new.year - year_of_birth
# return age
current_year = Time.new.year
current_month = Time.new.month
current_day = Time.new.day
if current_month == 2 && current_day == 29
current_month = 3
end
if current_month > month_of_birth
bday_passed = true
elsif current_month < month_of_birth
bday_passed = false
elsif current_month == month_of_birth
if current_day > day_of_birth
bday_passed = true
elsif current_day < day_of_birth
bday_passed = false
elsif current_day == day_of_birth
bday_passed = true
#happy_birthday = true
#unused in this function - if providing more detailed output than simply age, display happy birthday message "if happy_birthday"
end
end
if bday_passed
age = current_year - year_of_birth
elsif !bday_passed
age = current_year - year_of_birth - 1
end
return age
end