Giter VIP home page Giter VIP logo

say-hello-ruby-oc-000's Introduction

Say Hello

Objectives

  1. Recognize a default argument.
  2. Build a method that can take in an optional argument, with a default.

What Is A Default Argument?

We already know that methods can be defined to accept arguments:

def plus_five(number)
	number + 5
end

Above, we defined a method called plus_five that can accept any number and will return the sum of that number plus 5.

Defining our methods to accept arguments allows us to access specific pieces of information within our methods; it allows us to pass in information to our methods.

What would happen if you called the above method but forgot to pass in a number as an argument?

Drop into IRB, copy and paste the above method definition and then call the method like this:

plus_five

You should see the following error:

ArgumentError: wrong number of arguments (0 for 1)

What if you wanted to avoid this error? What if you wanted to supply your method with a default number to use, in the event that it is called without an argument?

Here's where default arguments come in.

With default arguments, you can specify default values for a method's arguments––values that the method will default to using if the caller doesn't pass them explicitly.

This is done using the assignment operator:

def plus_five(number=10)
	number + 5
end

Now, if you call your plus_five method without any arguments, the method will use the default argument of 10 and return 15.

Instructions

The second objective of this lab is to build a method called say_hello. This method should accept the argument of a person's name. It then should print "Hello " with the name followed by an exclamation point (don't forget that space after the "Hello"). For instance:

# I call on the method, say_hello, and give it the string "Gabriela" 
say_hello("Gabriela")

# The method prints this text to the screen:
Hello Gabriela!

There is one more thing this method should do: if you call on this method and forget to give it the name of a person, it should just say, "Hello Ruby Programmer!". Use a default argument to accomplish this!

Steps

  1. Run learn to see where you stand.
  • You probably got two NoMethodErrors. This means the test was looking for a method called say_hello but couldn't find it.
  1. Define the method in say_hello.rb
  • Run learn again. Are you getting a different error? If you haven't seen this error, guess what it's trying to tell you then Google it with the word "ruby".
  1. Get all the tests to pass!
  2. Once all of the tests are passing, use learn submit to submit your lab. Or alternatively, you can stage and commit your code; push it up to your fork; and submit a pull request.

say-hello-ruby-oc-000's People

Contributors

kthffmn avatar sophiedebenedetto avatar aviflombaum avatar markedwardmurray avatar ahimmelstoss avatar annjohn avatar fs-lms-test-bot avatar jmburges avatar msuzoagu avatar sarogers avatar fislabstest avatar xristocodos avatar

Watchers

James Cloos 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.