Explore Ruby's Methods & Enumerables




Lesson Objectives

During this lesson, students will be:

  • Use the ruby docs to find methods
  • Use and explain ruby methods

This morning you will each get a Ruby method to explore. Your job is to look in the Ruby documentation to read about the method, come up with an explanation of what it is, and show an example of it in use.

You'll have 15 minutes to research and prepare. You will then take turns sharing your screen and presenting your findings to the class. When you are not presenting, it's your job to listen, learn, take notes, and ask questions.

Google Sheet with assignments




String

  1. rstrip /rstrip!
  2. reverse /reverse!
  3. gsub / gsub!
  4. capitalize capitalize! / upcase / upcase!
  5. downcase/ downcase! / swapcase / swapcase!
  6. chomp / chomp!
  7. to_f / to_i
  8. to_str/ to_sym / to_a

Example string: "we researched ruby methods"




Array

  1. compact / compact!
  2. delete_at /delete_if
  3. fetch
  4. flatten / flatten!
  5. sample
  6. zip
  7. <<
  8. first / last
  9. step (note, you'll be doing the .step on a range of numbers, but it can then be used with an array)
  10. join
  11. reduce

Example array:

students = ["An", "Todd", "Mark", "Jim", "Cathleen", "Ky", "Biren", "Geraldine", "Hanna", "Dylan", "Sheila", "Charles", "Soniya", "Jerrica", "Ellen", "Lenin", "Adam", "Stanley", "Matthew", "Anthony", "Joe", "Emily", "Amanda"]



Hash

  1. has_key?
  2. keys
  3. merge / merge!
  4. flatten
  5. invert

Example hash:

cohort = {name: "Hopper", instructors: 4, "never_gonna_give_you_up" => "favorite song", most_played_song: "Freebird", instructors: ["Matt", "Thom", "Karolin", "Kristyn"], 100 => 200}



Integer

  1. odd? / even?
  2. floor / next
  3. times

Example integers:

1
28
12_345_678



Method

  1. Default values in parameters



Enumerables

The Enumerable module provides a set of methods to traverse, search, sort and manipulate collections

  1. for in
  2. each do
  3. each with index
  4. map
  5. select / select!



Post your notes and example in Slack

When it's your turn to present, post your topic, notes, and comment in Slack so that others can put it into their notes.

Make sure your notes include at least one code example.

Formatting for Slack:

formatting

Example

slack