-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathanswers.rb
145 lines (115 loc) · 3.56 KB
/
answers.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
144
145
# 1. fork this repo
# 2. clone your repo to your computer
# 3. make a new directory with your name
# 4. put your answers in this directory
# 5. make a commit for every question
# 6. make a pull request before time is up!!!
require 'pry'
# # Question 1
# Define a method called `offer_rose`, which should take one argument named `person` (String).
# When called the method should print to the terminal:
# "Would you take this rose, `person`, in exchange for giving an old beggar woman shelter from the bitter cold?"
# puts "What is your name? "
# offer_rose = gets.chomp
# print "Would you take this rose, " + offer_rose + " in exchange for giving an old beggar woman shelter from the bitter cold?"
# # Question 2
# Assume the following hash...
#
town = {
residents: ["Maurice", "Belle", "Gaston"],
castle: {
num_rooms: 47,
residents: ["Robby Benson"],
guests: []
}
}
# ```
# Using Ruby...
# - Remove "Belle" from `residents`
# - Add "Belle" to the `guests` array
# Type your solution directly below this line:
town[:residents].delete_at(1)
town[:castle][:guests].push("Belle")
# # Question 3
# Assume you have an array of strings representing friends' names...
# ```ruby
friends = ["Chip Potts", "Cogsworth", "Lumière", "Mrs. Potts"]
# ```
# Using a loop and string interpolation, print each string in `friends` to the Terminal...
# ```ruby
# "Belle is friends with Chip Potts"
# "Belle is friends with Cogsworth"
# "Belle is friends with Lumière"
# "Belle is friends with Mrs. Potts"
# ```
friends.each do |friend|
p "Belle is friends with #{ friend }"
end
# # Question 4
# Assume the following array of hashes:
# ```ruby
lost_boys = [
{name: 'Tootles', age: 11},
{name: 'Nibs', age: 9},
{name: 'Slightly', age: 10},
{name: 'Curly', age: 8},
{name: 'The Twins', age: 9}
]
# ```
# Use `.each` to iterate over the `lost_boys` array and increase each boy's age by 30 years.
lost_boys.each do |boys|
boys[:age] +=30
end
# # Question 5
# Assume the following array:
# ```ruby
children = ['Wendy', 'John', 'Michael']
# ```
# Use `.map` to iterate through the `children` array and add ` Darling` to the end
# of their names. Assign the returned array to a variable called `darling_children`.
# Example: `Wendy` should become `Wendy Darling` in the new array.
children.map { |name| name + " Darling" }
# # Question 6
# Define a Ruby class called `Animal`. Each `Animal` should have...
# - A `name` (String) attribute
# - A `greet` instance method
# - The ability to "get" and "set" `name`
class Animal
def initialize (name)
@name = name
end
def greet
p "Hello"
end
def get
p @name
end
def set (new_name)
@name = new_name
end
end
# # Question 7
# Create a new `Animal` instance with the name "Pumba" an assign it to a variable named `pumba`.
pumba = Animal.new("Pumba")
# # Question 8
# Write a method called `toonify` that takes two parameters, `accent` and `sentence`.
# - If `accent` is the string `"daffy"`, return a modified version of `sentence` with all "s" replaced with "th".
# - If the accent is `"elmer"`, replace all "r" with "w".
# - Feel free to add your own accents as well!
# - If the accent is not recognized, just return the sentence as-is.
# ```ruby
# toonify "daffy", "so you smell like sausage"
# #=> "tho you thmell like thauthage"
# ```
# Call the method twice with different arguments
def toonify (accent, sentence)
if (accent == 'daffy')
daffy_new = sentence.gsub!('s', 'th')
return daffy_new
elsif (accent == 'elmer')
elmer_new = sentence.gsub!('r', 'w')
return elmer_new
else
return sentence
end
end