-
Notifications
You must be signed in to change notification settings - Fork 451
/
Copy pathruby_language.step
184 lines (139 loc) · 5.52 KB
/
ruby_language.step
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
goals do
goal "Be able to use the basic building blocks of Ruby code"
goal "Use IRB to run Ruby code"
goal "Do simple calculations"
goal "Use and understand variables"
goal "Use and understand arrays"
goal "Use loops and conditional statements"
end
steps do
step do
message "Type this in the terminal to start the Interactive Ruby Shell, a program which lets you try out Ruby code:"
console_without_message "irb"
message "Yours might look different, but it should look something like this:"
console_without_message "irb(main):001:0>"
end
step do
console_with_message "Next try some simple math that's built into Ruby. Type these lines into IRB:", "3 + 3\n7 * 6"
end
step do
message "**Variables** are names with values assigned to them."
console_without_message "my_variable = 5"
message "This assigns the value `5` to the name `my_variable`."
end
step do
message "You can also do math with variables:"
console_without_message <<-RUBY
my_variable + 2
my_variable * 3
RUBY
end
step do
message "Variables can also hold more than one value. This is called an **array**."
console_without_message 'fruits = ["kiwi", "strawberry", "plum"]'
message "Here we're using the variable `fruits` to hold a collection of fruit names."
end
step do
console <<-RUBY
fruits = fruits + ["orange"]
fruits = fruits - ["kiwi"]
RUBY
message "`+` and `-` are called operators. We can use them with the array of fruits just like we can use them with numbers."
end
step do
message "A **hash** is a dictionary-like collection of unique **keys** and their **values**."
console <<-RUBY
fruit_dictionary = { strawberry: "a sweet soft red...", plum: "an oval, purple fruit...", orange: "a round juicy citrus fruit..." }
RUBY
message "We're creating a hash which associates the name of a fruit (**key**) with the definition of the fruit (**value**)."
end
step do
console <<-RUBY
fruit_dictionary[:strawberry]
RUBY
message "Here we're using the **key**, `:strawberry`, to look up the associated **value**, the definition of 'strawberry' in the fruit dictionary."
end
step do
message "Everything in Ruby has a **class**. Type this into IRB:"
console_without_message <<-RUBY
7.class
"kiwi".class
fruits.class
RUBY
message "These are the four data types introduced so far: **Fixnum** (numbers), **String** (text), **Array** (lists), and **Hash** (dictionaries)."
end
step do
message "Each class has different **methods** that can be used on **instances** of that class."
console_without_message <<-RUBY
fruits.length
fruits.first
RUBY
message "You can see all the methods available for an object:"
console_without_message <<-RUBY
fruits.methods
RUBY
message "And you can call multiple methods in a row:"
console_without_message <<-RUBY
fruits.methods.sort
RUBY
message <<-MARKDOWN
In `fruit.methods.sort` above, `fruit.methods` returns an array, and `.sort` sorts that array. It's exactly like this, but without the `array` variable:
MARKDOWN
console_without_message <<-RUBY
array = fruits.methods
array.sort
RUBY
end
step do
message "Arrays have a method called **each** which iterates through the list running code on each item."
console_without_message <<-RUBY
fruits.each do |fruit|
puts fruit
end
RUBY
message "This takes the first item from the `fruits` array (`\"strawberry\"`), assigns it to the variable `fruit`, and runs the code between `do` and `end`. Then it does the same thing for each other item in the list. The code above should print a list of the fruits."
end
step do
message "A **conditional** runs code only when a statement evaluates to true."
console_without_message <<-RUBY
if my_variable > 1
puts "YAY!"
end
RUBY
message "This prints `YAY!` if the value stored in `my_variable` is greater than 1."
message "Try changing the `>` in the conditional to a `<`."
message "If you want to do something else when the statement evaluates to false, you can use an `else`:"
console_without_message <<-RUBY
if my_variable > 1
puts "YAY!"
else
puts "BOO!"
end
RUBY
end
step do
message "You can also make your own methods:"
console_without_message <<-RUBY
def pluralize(word)
word + "s"
end
pluralize("kiwi")
RUBY
message "Methods take **parameters**, which are the variables they work on. In this case, we made a method called pluralize that takes one parameter, a word."
message "Methods can also return data. In this case, pluralize returns the word with an 's' added to the end of it. In Ruby, methods return whatever the last line of the method evaluates to."
end
step do
message "This is an *optional* small group practice question! Work in groups of 2-3 to solve the problem."
message "Write some Ruby that prints out the names of the people in your group."
message <<-MARKDOWN
Hints:
1. Start by opening up `irb`.
1. Create the names as strings in an array.
1. Store that array to a variable.
1. Then use the `.each` method on the stored array to loop through each of the names.
1. Use the `puts` method to print out the names.
MARKDOWN
end
end
important "Before you move on to the next step you must exit IRB by typing 'exit'"
next_step "getting_started"