Last active
December 22, 2021 18:14
-
-
Save madeleine68/92bfeea93d178580e98289e426815a50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q1. | |
# "def color" is a regular method and it will create a new instance method, when used without an explicit receiver. | |
# In the context of a class, self refers to the current class. Defining a method on self.shape creates a class method. | |
# def bar.radius creates singleton method attached to the object bar. | |
# The difference between class methods and singleton methods is that class methods are available to all instances of | |
# a class object while singleton methods are available only to that single instance. | |
------------------------------------------------------------------------------------ | |
Q2. | |
module Enumerable | |
def each_third(n) | |
(n - 1).step(self.size - 1, n).map { |i| "The circle is #{self[i]}" } | |
end | |
end | |
puts ['white','yellow', 'blue', 'red', 'green', 'black', 'brown', 'azure'].each_third(3) | |
------------------------------------------------------------------------------------ | |
Q3. | |
SELECT * FROM employee | |
JOIN cars ON | |
employee.card_id = cars.id | |
AND employee.card_id IS NOT NULL | |
------------------------------------------------------------------------------------ | |
Q4. | |
def logic_q(value) | |
case value | |
when 1 | |
puts "your return value is 0 " | |
when 0 | |
puts "your return value is 1 " | |
else | |
puts "Number must be 0 or 1" | |
end | |
end | |
------------------------------------------------------------------------------------ | |
Q5. | |
def color_picker (num) | |
m3 = num.modulo(3) == 0 | |
m5 = num.modulo(5) == 0 | |
puts case | |
when (m3 and m5) then 'blackwhite' | |
when m3 then 'white' | |
when m5 then 'black' | |
else num | |
end | |
end | |
------------------------------------------------------------------------------------ | |
Q6. | |
amount = base * percentage | |
summary = 2 * basis(amount) + extra(amount) * 1.05 | |
if (province == 'BC') | |
percentage = BC_PERCENTAGE | |
elsif (province == 'MB') | |
percentage = MB_PERCENTAGE | |
points = 2 | |
elsif (province == 'NB') | |
percentage = NB_PERCENTAGE | |
else | |
percentage = 1 | |
amount = base | |
end | |
------------------------------------------------------------------------------------ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment