Answer
270 | 271 | ``` 272 | irb(main):020:0> answer = 7 * 6 273 | => 42 274 | irb(main):021:0> answer * 10 275 | => 420 276 | irb(main):022:0> answer 277 | => 42 278 | ``` 279 | 280 |Answer
309 | 310 | ``` 311 | irb(main):023:0> answer = 7 * 6 312 | => 42 313 | irb(main):024:0> "The answer to life, the universe and everything is #{answer}" 314 | => "The answer to life, the universe and everything is 42" 315 | ``` 316 | 317 |Answer
363 | 364 | ``` 365 | irb(main):030:0> "!gniog peeK !llew repus gniod er'uoY".reverse 366 | => "You're doing super well! Keep going!" 367 | ``` 368 | 369 |Answer
105 | 106 | ```ruby 107 | puts("Enter a string to reverse: ") 108 | input = gets 109 | puts("Your string reversed is: ") 110 | puts(input.reverse) 111 | ``` 112 | 113 |Answer
138 | 139 | ```ruby 140 | puts("Enter the first number:") 141 | x_string = gets 142 | x_number = x_string.to_i 143 | puts("Enter the second number:") 144 | y_string = gets 145 | y_number = y_string.to_i 146 | puts("#{x_number} * #{y_number} = #{x_number * y_number}") 147 | ``` 148 | 149 |Answer
205 | 206 | ```ruby 207 | def convert_to_dog_years(age) 208 | return age * 7 209 | end 210 | 211 | puts("How old is your dog in human years?") 212 | age_human_years_string = gets 213 | age_human_years_number = age_human_years_string.to_i 214 | 215 | age_dog_years = convert_to_dog_years(age_human_years_number) 216 | puts("Your dog is #{age_dog_years} in dog years") 217 | ``` 218 | 219 |
224 |
225 | So far, each line of code we've written has run exactly once (each time a
226 | script runs). Ruby goes through each line in the script, runs it, then moves
227 | on to the next line. When it reaches the end of the file, the script exits.
228 |
229 | One of the things that computers are really good at is repeating themselves.
230 | In Ruby we can use methods like `loop` and `.times` to make the computer
231 | repeat things.
232 |
233 | You might want to try some of the following out in `irb` to get a feel for
234 | how they work. (If you get stuck in a loop in `irb` you can escape it by
235 | pressing `ctrl + c`):
236 |
237 | ```ruby
238 | 5.times do
239 | puts("Odelay!")
240 | end
241 | ```
242 |
243 | ```ruby
244 | 10.times do |i|
245 | puts("#{10 - i} green bottles, hanging on the wall,")
246 | puts("#{10 - i} green bottles, hanging on the wall,")
247 | puts("And if one green bottle should accidentally fall,")
248 | puts("There'll be #{9 - i} green bottles hanging on the wall.")
249 | puts
250 | end
251 | ```
252 |
253 | ```ruby
254 | loop do
255 | puts("What's your name?")
256 | name = gets
257 | puts("Hello #{name} (to exit this infinite loop, press ctrl+c)")
258 | end
259 | ```
260 |
261 | The bit between the `do` and the `end` is called a block, which is run by the
262 | method repeatedly. Blocks can take parameters after the `do`, which are
263 | wrapped in vertical pipes like `|i|`.
264 |
265 | #### Task 6: Update one of your scripts to work in a loop
266 |
267 | Earlier, you wrote a few scripts that ask the user for some input, do
268 | something with the input then print the result and exit. Change one of these
269 | scripts (e.g. `multiply.rb`) so they do this in an infinite loop (using `loop`).
270 |
271 | If you get stuck in an infinite loop (a loop which never ends) in the command
272 | line you can break out by pressing `ctrl + c`.
273 |
274 | Answer
276 | 277 | ```ruby 278 | loop do 279 | puts("Enter the first number:") 280 | x_string = gets 281 | x_number = x_string.to_i 282 | puts("Enter the second number:") 283 | y_string = gets 284 | y_number = y_string.to_i 285 | puts("#{x_number} * #{y_number} = #{x_number * y_number}") 286 | end 287 | ``` 288 | 289 |Answer
365 | 366 | ```ruby 367 | def multiplication_challenge 368 | x = rand(12) + 1 369 | y = rand(12) + 1 370 | 371 | print("What is #{x} multiplied by #{y} ? ") 372 | user_input = gets 373 | user_input_as_a_number = user_input.to_i 374 | 375 | correct_answer = x * y 376 | if user_input_as_a_number == correct_answer 377 | puts(":) correct!") 378 | return true 379 | else 380 | puts(":( oops!") 381 | puts("The answer was #{correct_answer}") 382 | return false 383 | end 384 | end 385 | 386 | number_of_rounds = 4 387 | score = 0 388 | 389 | puts("Times tables challenge") 390 | puts("----------------------") 391 | puts 392 | puts("You will be asked #{number_of_rounds} questions.") 393 | puts("Press enter to start...") 394 | gets 395 | 396 | number_of_rounds.times do |i| 397 | if multiplication_challenge 398 | score = score + 1 399 | end 400 | end 401 | 402 | if score == number_of_rounds 403 | puts("You scored #{score}/#{number_of_rounds}. Well done!") 404 | else 405 | puts("You scored #{score}/#{number_of_rounds}. Better luck next time!") 406 | end 407 | ``` 408 | 409 |Answer
480 | 481 | ``` 482 | irb(main):001:0> require("date") 483 | => true 484 | 485 | irb(main):002:0> birthday = Date.new(1988, 1, 20) 486 | => #Answer
534 | 535 | ``` 536 | irb(main):006:0> file = File.read("/Users/your-username/Desktop/lesson-1-html-and-css/index.html") 537 | => "\n... 538 | irb(main):007:0> file.size 539 | 1260 540 | ``` 541 | 542 |
25 |
26 |
27 | You'll have a basic understanding of how web applications work, and a good foundation to build on.
28 |
29 | ## Introduction to HTTP
30 |
31 | ### What is HTTP?
32 |
33 | HTTP stands for HyperText Transfer Protocol.
34 |
35 | HTTP describes the format of the communications between your web browser and
36 | a "web server" (which is just a kind of computer).
37 |
38 | Your browser sends HTTP requests and the server responds with HTTP responses.
39 |
40 | For example, when you visit `www.gov.uk` your browser will send a request a like this:
41 |
42 | ```http
43 | GET / HTTP/1.1
44 | Host: www.gov.uk
45 | ```
46 |
47 | And GOV.UK's server will respond with something like:
48 |
49 | ```
50 | HTTP/1.1 200 OK
51 | Date: Wed, 29 May 2019 12:08:17 GMT
52 | Content-Type: text/html; charset=UTF-8
53 | Content-Length: 32897
54 |
55 |
56 |
57 | ... snip ...
58 |
59 | ```
60 |
61 | In this case the response contains the HTML of the GOV.UK homepage.
62 |
63 | ### Viewing HTTP requests using browser developer tools
64 |
65 | Modern browsers come with built in tools that let you see what's happening
66 | under the hood. In Google Chrome on a Mac you can bring these up for any
67 | webpage by pressing Command + Option + I (`⌘ + ⌥ + I`).
68 |
69 | 
70 |
71 | The Network tab shows you the requests and responses your browser is sending and receiving.
72 |
73 | #### Task 3: Use the developer tools to look at an HTTP request
74 |
75 | Go to any website you like and use the developer tools to look at the first
76 | HTTP request it makes.
77 |
78 | Answer (screenshots)
80 | 81 |  82 |  83 | 84 |Answer
192 | 193 | You should see a web page that says "Hello browser!" and you should see some lines on the command line like this: 194 | 195 | ``` 196 | [2019-05-29 21:38:46] INFO WEBrick 1.3.1 197 | [2019-05-29 21:38:46] INFO ruby 2.3.7 (2018-03-28) [universal.x86_64-darwin17] 198 | [2019-05-29 21:38:46] INFO WEBrick::HTTPServer#start: pid=86541 port=8080 199 | Hello server! 200 | ::1 - - [29/May/2019:21:38:58 BST] "GET / HTTP/1.1" 200 14 201 | - -> / 202 | Hello server! 203 | ::1 - - [29/May/2019:21:38:59 BST] "GET /favicon.ico HTTP/1.1" 200 14 204 | http://localhost:8080/ -> /favicon.ico 205 | Hello server! 206 | ::1 - - [29/May/2019:21:39:03 BST] "GET / HTTP/1.1" 200 14 207 | - -> / 208 | Hello server! 209 | ::1 - - [29/May/2019:21:39:03 BST] "GET /favicon.ico HTTP/1.1" 200 14 210 | http://localhost:8080/ -> /favicon.ico 211 | ``` 212 | 213 | Note that `puts("Hello server!")` still prints to the command line, and that 214 | the `response.body` is what we see in the browser. 215 | 216 |Hello browser!
" 229 | end 230 | ``` 231 | 232 | It can get difficult to maintain if we put all our HTML in ruby strings like 233 | we did in the example above. It would be much nicer if we could have our HTML 234 | in html files and our Ruby in ruby files. Fortunately, we can achieve this 235 | with `File.read`: 236 | 237 | 238 | ```ruby 239 | server.mount_proc("/home") do |request, response| 240 | response.content_type = "text/html; charset=utf-8" 241 | response.body = File.read("index.html") 242 | end 243 | ``` 244 | 245 | (This assumes there's a file called `index.html` in your command line's working directory) 246 | 247 | #### Task 6: Serve an HTML file with WEBRick 248 | 249 | * Copy `lesson-1-html-and-css/index.html` into `lesson-4-ruby-on-the-web` 250 | * Create a copy of `hello-world-server.rb` called `barking-permit-server.rb` 251 | * Using the example above, make the server respond with the text from `index.html` 252 | * Run the server with `ruby barking-permit-server.rb` and visit [http://localhost:8080/home](http://localhost:8080/home) to check it works 253 | * When you're done, stop the server with `CTRL + C` 254 | 255 |Answer
257 | 258 | ```ruby 259 | require("webrick") 260 | server = WEBrick::HTTPServer.new(Port: 8080, DocumentRoot: './public') 261 | 262 | server.mount_proc("/home") do |request, response| 263 | response.body = File.read("index.html") 264 | end 265 | 266 | server.start 267 | ``` 268 | 269 |Answer
308 | 309 | ```ruby 310 | require("webrick") 311 | server = WEBrick::HTTPServer.new(Port: 8080, DocumentRoot: './public') 312 | 313 | server.mount_proc("/home") do |request, response| 314 | dog_name = request.query["dog-name"] 315 | dog_age = request.query["dog-age"] 316 | 317 | if dog_name 318 | response.body = "The dog #{dog_name} is #{dog_age} years old" 319 | else 320 | response.body = File.read("index.html") 321 | end 322 | end 323 | 324 | server.start 325 | ``` 326 | 327 |Favourite Fruit
346 | 347 |My favourite fruit is FAVOURITE_FRUIT_NAME. I like it the best because it is FAVOURITE_FRUIT_REASON.
348 | ``` 349 | 350 | We could load this file up, and then substitute the placeholders like this: 351 | 352 | ```ruby 353 | File.read("fruits.html") 354 | .sub("FAVOURITE_FRUIT_NAME", "Banana") 355 | .sub("FAVOURITE_FRUIT_REASON", "easy to peel") 356 | ``` 357 | 358 | Which would return: 359 | 360 | ```html 361 |Favourite Fruit
362 | 363 |My favourite fruit is Banana. I like it the best because it is easy to peel.
364 | ``` 365 | 366 | #### Task 8: Template barking permit 367 | 368 | Create a file called `barking-permit-template.html` in `lesson-4-ruby-on-the-web`. 369 | 370 | Add the following HTML to the file (it's fine to copy-paste this): 371 | 372 |HTML for `barking-permit-template.html`
374 | 375 | ```html 376 | 377 | 378 | 379 |Apply for a Barking Permit
396 |400 | Application complete 401 |
402 |Answer
435 | 436 | ```ruby 437 | require("webrick") 438 | server = WEBrick::HTTPServer.new(Port: 8080, DocumentRoot: './public') 439 | 440 | server.mount_proc("/home") do |request, response| 441 | dog_name = request.query["dog-name"] 442 | dog_age = request.query["dog-age"] 443 | 444 | if dog_name 445 | response.body = File.read("barking-permit-template.html") 446 | .sub("DOG_NAME", dog_name) 447 | .sub("DOG_AGE_HUMAN_YEARS", dog_age) 448 | else 449 | response.body = File.read("index.html") 450 | end 451 | end 452 | 453 | server.start 454 | ``` 455 | 456 |Answer
485 | 486 | ```ruby 487 | require("webrick") 488 | require("date") 489 | 490 | def convert_to_dog_years(age_human_years) 491 | return age_human_years * 7 492 | end 493 | 494 | server = WEBrick::HTTPServer.new(Port: 8080, DocumentRoot: './public') 495 | 496 | server.mount_proc("/home") do |request, response| 497 | dog_name = request.query["dog-name"] 498 | dog_age = request.query["dog-age"] 499 | 500 | if dog_name 501 | response.body = File.read("barking-permit-template.html") 502 | .sub("DOG_NAME", dog_name) 503 | .sub("DOG_AGE_HUMAN_YEARS", dog_age) 504 | .sub("DOG_AGE_DOG_YEARS", convert_to_dog_years(dog_age.to_i).to_s) 505 | .sub("PERMIT_ISSUED_ON", Date.today.strftime("%d/%m/%Y")) 506 | .sub("PERMIT_VALID_UNTIL", (Date.today + 1000).strftime("%d/%m/%Y")) 507 | else 508 | response.body = File.read("index.html") 509 | end 510 | end 511 | 512 | server.start 513 | ``` 514 | 515 |
26 |
27 | Screenshots showing how to add a folder in Visual Studio Code
107 |
108 |
109 | Screenshot showing how to add a file in Visual Studio Code
115 |
116 | Screenshot showing how to open the HTML file in Google Chrome
: 130 |
131 | Heading
176 |Heading
177 |Heading
178 |Heading
179 |Heading
180 |Heading
181 | ``` 182 | 183 | A `h1` defines the most important heading whereas a `h6` defines the least important. 184 | 185 | Headings should go from `h1` to `h6` in order - always start from ``, next use ``, and so on.
186 |
187 | It's important to not skip one or more heading levels - this is because screenreaders can jump from heading to heading and leaving a heading level out may lead to a user being confused.
188 |
189 |
190 | Don't:
191 |
192 | ```html
193 | Heading 1
194 | Heading 3
195 | Heading 4
196 | ```
197 |
198 | Do:
199 |
200 | ```html
201 | Heading 1
202 | Heading 2
203 | Another heading 2
204 | Yet another heading 2
205 | Heading 3
206 | ```
207 |
208 | Headings can skip a level when going back up to a more important heading level:
209 |
210 | ```html
211 | Heading 1
212 | Heading 2
213 | Heading 3
214 | Heading 4
215 | Another heading 2
216 | Another heading 3
217 | ```
218 |
219 | #### Task 3: add a heading
220 |
221 | Add a `h1` heading tag, which includes the phrase `Apply for a Barking Permit`, inside the `body` tag of your page.
222 |
223 | ### Element: paragraph (`p`)
224 |
225 | Putting content into a `
Heading 1
194 |Heading 3
195 |Heading 4
196 | ``` 197 | 198 | Do: 199 | 200 | ```html 201 |Heading 1
202 |Heading 2
203 |Another heading 2
204 |Yet another heading 2
205 |Heading 3
206 | ``` 207 | 208 | Headings can skip a level when going back up to a more important heading level: 209 | 210 | ```html 211 |Heading 1
212 |Heading 2
213 |Heading 3
214 |Heading 4
215 |Another heading 2
216 |Another heading 3
217 | ``` 218 | 219 | #### Task 3: add a heading 220 | 221 | Add a `h1` heading tag, which includes the phrase `Apply for a Barking Permit`, inside the `body` tag of your page. 222 | 223 | ### Element: paragraph (`p`) 224 | 225 | Putting content into a `` tag will break your text up into paragraphs. 226 | This helps make the content of your page easier to read for the user. 227 | 228 | #### Task 4: add a paragraph 229 | 230 | Add the following paragraph inside your `
` tag, after the ``: 231 | 232 | ```html 233 |
234 | The ministry of dogs is trying a new pilot. Good dogs can apply for a 235 | barking permit so they can woof whenever they like. It’s not yet clear how 236 | this permit will be enforced. 237 |
238 | ``` 239 | 240 | ### Element: link (`a`) 241 | 242 | A link lets the user go to another webpage. We use the attribute `href` to indicate where you want the user to go. 243 | 244 | The link tag is called `a` for "anchor". 245 | 246 | #### Task 5: add a link 247 | 248 | Add a link to the end of your paragraph: 249 | 250 | ```html 251 | 252 | More information about barking 253 | 254 | ``` 255 | 256 | ### Element: div (`div`) 257 | 258 | A div tag lets you group elements together. Grouping elements is useful as we can later style them together (e.g. giving them all the same colour). 259 | 260 | #### Task 6: add a div 261 | 262 | Wrap your existing paragraph, link and heading in a div: 263 | 264 | ```html 265 |Apply for a Barking Permit
267 |268 | The ministry of dogs is trying a new pilot. Good dogs can apply for a barking permit so they can woof whenever and wherever they like. It’s not yet clear how this permit will be enforced. 269 | More information about barking. 270 |
271 |
291 | *
292 |
293 | Images are primarily made up of three components:
294 |
295 | * the `
308 | ```
309 |
310 | Here you can see we have told the `src` of the image to look in the images
311 | folder and display the image `shiba-inu.jpeg`, then we have given it a
312 | description in the `alt` attribute.
313 |
314 | ## Forms
315 |
316 | Forms let you ask your user for input. There are a number of interactive
317 | components (like text boxes and buttons) you can use in forms.
318 |
319 | ### Element: form (`form`)
320 |
321 | The `