/
code.rb
137 lines (122 loc) · 2.78 KB
/
code.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
#!/usr/bin/ruby
# Takes string, prints upcase, and puts downcased and reversed
# Params:
# +str+:: String to print
# Returns nothing
def echo(str)
print str.upcase
puts str.downcase.reverse
end
# Asks for input
# Returns chomped result
def input
puts 'input'
gets.chomp
end
# Iterate from 1..100 and puts result
# Returns nothing
def for_loop
[1..100].each do |i|
puts i.to_s
end
end
# Simple elseif case
# Params:
# +str+:: Name to put in elseif
# Returns nothing
def if_elsif(str)
if str.to_s == 'David'
print "Hej #{str}"
elsif str.to_s == 'Jackie'
print 'Du aer fan mys'
else
print "Halla #{str}"
end
end
# Check if object is able to downcase
# Params:
# +object+:: Object to check
# Returns true if downcaseable, false if not
def respond_to_downcase(object)
object.respond_to?(:downcase)
end
# Concatenates second_string to first_string
# Params:
# +first_string+:: The string to concatenate with
# +second_string+:: The string to concatenate from
# Returns result of concatenation
def concatenation_operator(first_string, second_string)
first_string << second_string
end
# Iterates over list, concatenate every iteration with str and put to stdout
# Params:
# +str+:: String to preface every iteration with
# +list+:: List to iterate over
# Returns nothing
def splatty(str, *list)
list.each { |item| puts str + ' ' + item }
end
# Action if-statement
# Takes no parameters
# Puts "Hej" if 2 is more than 1
# Returns nothing
def action_if_statement
puts 'Hej' if 2 > 1
end
# Switch-case statement
# Params:
# +var+:: String to test
# Returns nothing
def switch_case(var)
case var
when '1' then puts '1'
when '2' then puts '2'
else
puts 'other'
end
end
# Block statement, turn array of str to symbols
# Returns array of symbols
def block_func
# Rubocop gillar inte strings.collect { |x| x.to_sym }
array_of_strings.collect(&:to_sym)
end
# Proc statement, turn array of str to symbols
# Returns array of symbols
def proc_func
symbolize = proc { |x| x.to_sym }
array_of_strings.collect(&symbolize)
end
# Lambda statement, turn array of str to symbols
# Returns array of symbols
def lambda_func
symbolize = ->(x) { x.to_sym }
array_of_strings.collect(&symbolize)
end
# Gets an array of strings
# Returns array of strings
def array_of_strings
%w(leonardo donatello raphael michaelangelo)
end
# Class Car
class Car
# Initializes car with values
# Params:
# +brand+:: brand of car
# +model+:: model of car
# Returns instance of car
def initialize(brand, model)
@brand = brand
@model = model
end
# Starts engine, and puts result of engine state
# Returns nothing
def start_engine
if @engine_state
puts "#{@brand} #{@model} is already running"
else
@engine_state = true
puts 'Engine idle'
end
end
end