Code Monkey home page Code Monkey logo

kwk-level-1-lecture-004-ruby-2-recap-and-methods's Introduction

Ruby 2: Recap and Methods

Objective

Students get a review of the core concepts learned in the earlier lesson, puts, strings, math, and user input. Students get introduced to methods.

SWBAT

  • METHODS - Define a method in ruby.
  • METHODS - Call a method in ruby.

Recap

Feel free to use Ruby Recap and Methods Deck

  • Programs are Just Files

  • Strings Review

  • Reading Errors Review

CFU

Define puts and strings with an elbow partner. Share whole group.

Methods

In order to not repeat ourselves all the time, we can grab a sequence of commands and put them inside of a method. The method's name becomes a reference to all those commands and every time we call that method, the code inside of it will run.

Have students open irb and play with some method definitions

Display the following as an example:

def about_me
  puts "My name is Karlie"
  puts "I grew up in St. Louis"
  puts "I'm 25 Years Old"
  puts "My favorite food is Kookies"
end

Just defining a method does nothing, it only creates the method. When we want to run it, we have to call it by the name we gave it.

Students can practice writing an about me on white boards along with instructors.

def about_me
  puts "My name is Karlie"
  puts "I grew up in St. Louis"
  puts "I'm 25 Years Old"
  puts "My favorite food is Kookies"
end

about_me
#> My Name is Karlie
#> I grew up in St. Louis
#> I'm 25 Years Old
#> My favorite food is Kookies

Students should all build an about_me method.

Students should work on Dance Instructions Lab about 35 minutes.

After the lab, give one more IRB demo to setup for tomorrow.

Wrap Up

Okay, so we've learned about building programs, giving commands, strings, math, variables, and methods.

Let's look at some issues with methods in irb. Students can follow along

def two_step
  puts "Step to the left."
  puts "Step to the right."
end

two_step
two_step
two_step

How would we tell the two_step to just keep on going, to never stop, just keep on repeating? Do the dance, it never stops (We're leading toward loops).

Then also, let's think about this example. Students should follow along

def greeting
  puts "Hi Jane, I'm Karlie, how's your afternoon?"
end

greeting

In that greeting, we have 3 or 4 things that could be replaced with variables. If we wanted our greeting method to be really flexible, we might think of the greeting string as really being

local_greeting your_name, I'm my_name, how's your time_of_day?

local_greeting might be "What's up", or "Hey", or "Yo", or "Konichiwa"

your_name might be "Alice" or "Shirley"

my_name might be "Grace" or "Katherine"

time_of_day might be "morning" or "night" or "afternoon"

We might be able to use variables for this, let's try it, in IRB, students following along.

local_greeting = "Shalom"
your_name = "Golda"
my_name = "Lea"
time_of_day = "life"

# The goal being to get a greeting of:
#> "Shalom Golda, I'm Lea, how's your life?"

def greeting
  puts "#{local_greeting} #{your_name}, I'm #{my_name}, how's your #{time_of_day}?"
end

DON'T HIT ENTER, Just have the setup. Ask the students what should happen? The variables are defined, the method is defined, as long as the method can read those variables, everything should be fine, right?

Now try it.

local_greeting = "Shalom"
your_name = "Golda"
my_name = "Lea"
time_of_day = "life"

# The goal being to get a greeting of:
#> "Shalom Golda, I'm Lea, how's your life?"

def greeting
  puts "#{local_greeting} #{your_name}, I'm #{my_name}, how's your #{time_of_day}?"
end

greeting 

The program gives an error.

Undefined local variable or method `local_greeting` in `greeting`.

Computers never lie, if it says it can't find the variable, it can't find it. So something is missing. We'll cover this tomorrow, get excited!!! (Leading toward arguments).

kwk-level-1-lecture-004-ruby-2-recap-and-methods's People

Contributors

ashleyminor avatar danielseehausen avatar jcasimir avatar sammarcus avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.