Coder Social home page Coder Social logo

upperline-hs-ruby-2-parrot-lab's Introduction

##Polly Wants a Cracker?

parrot

In this lab you'll be training some parrots with special talents and get some practice creating your own methods.

Parrots love to mimic their human companions. In this lab you'll give your parrot methods a word or phrase as an argument and your parrot will take your argument, do something to it, and say then it back to you in their own quirky way. Read on for some of the key concepts you'll need to use to complete this lab.

###Method Arguments

We define methods with arguments as a way of to give our methods an extra piece of information or data to work with. An argument can be any of the data types When you're defining a method the arguments are just placeholders that represent what you'll pass in when you call your method.

def my_method_name(argument)
  argument.upcase
end

puts my_method_name("In this example my argument is a string!")

In the example above I would see this printed to my terminal: IN THIS EXAMPLE MY ARGUMENT IS A STRING!

We can also save arguments to a variable and pass that in to our method call. The example below will have the same result as the example above.

def my_method_name(argument)
  argument.upcase
end

sentence = "In this example my argument is a string!"
puts my_method_name(sentence)

###Return Values

A method's return value is always the last line of code before the "end" keyword. This is important to keep in mind if you're using puts inside of your method. If you use puts inside your method what you see printed to the screen may be different than your return value. You may sometimes see the last line with a "return" keyword in front of it, but it's not necessary to include this. In Ruby the last line is always the return value whether or not you write out the word "return".

  def say_pet_name(pet_name)
    puts "My pet's name is #{pet_name}"
    "My pet's name is Ralph"
  end

  puts say_pet_name("Coco")

If you run the above example you'll actually see two lines printed to the screen.

My pet's name is Coco My pet's name is Ralph

The puts inside of the method is printing the line with the pet_name argument string interpolated, while the second line is the result of puts-ing the return value of the say_pet_name method.

###Method Chaining

You can call multiple methods on a number or string by just adding another .method onto the end. We call this method chaining. When you chain on methods they change the thing they're called on one at a time from left to right. That means that the second chained method is actually changing the return value of the method that comes before it.

puts "matz is my homeboy".capitalize.reverse
puts "matz is my homeboy".reverse.capitalize

If you run these two lines of code you'll see two lines printed to the screen:

yobemoh ym si ztaM
Yobemoh ym si ztam

You can quickly see that the two lines to not create the same output. This is just one more reason why it's so important to keep track of the return values of your methods.

Okay, refresher over!

Fork and clone this lab and follow along and write your code in the parrot.rb file. All of the instructions for this lab are within the parrot.rb file. Try running your program when you are done.

Resources

View Polly Wants a Cracker? on Learn.co and start learning to code for free.

upperline-hs-ruby-2-parrot-lab's People

Contributors

dfenjves avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.