Coder Social home page Coder Social logo

method-arguments-lab-v-000's Introduction

Method Arguments Lab

Objectives

  1. Define a method that takes in an argument and uses that argument in the method body.
  2. Define a method that takes in two arguments and uses both arguments in the method body.

Instructions

You'll be coding your methods in lib/introduction.rb.

The #introduction Method

Run the test suite to get started. To do that, run learn or learn test in your terminal. Let's take a look at the first error:

Failures:

  1) #introduction takes in an argument of a name and puts out a phrase with that name using string interpolation
     Failure/Error: expect{introduction("Josh")}.to output("Hi, my name is Josh.\n").to_stdout
     NoMethodError:
       undefined method `introduction' for #<RSpec::ExampleGroups::Introduction:0x007fdbc499a938>
     # ./spec/introduction_spec.rb:5:in `block (3 levels) in <top (required)>'
     # ./spec/introduction_spec.rb:5:in `block (2 levels) in <top (required)>'

Wow, that's a lot of information. The important part for us though is the line that tells us what kind of error we are experiencing:

NoMethodError:
       undefined method `introduction`

Looks like our test is expecting to test a method called #introduction. Let's define that method in lib/introduction.rb.

# lib/introduction.rb

def introduction
end

Now we'll run our test suite again. You should see the following error:

Failures:

  1) #introduction takes in an argument of a name and puts out a phrase with that name using string interpolation
     Failure/Error: expect{introduction("Josh")}.to output("Hi, my name is Josh.\n").to_stdout
     ArgumentError:
       wrong number of arguments (1 for 0)
     # ./lib/introduction.rb:9:in `introduction'
     # ./spec/introduction_spec.rb:5:in `block (3 levels) in <top (required)>'
     # ./spec/introduction_spec.rb:5:in `block (2 levels) in <top (required)>'

Once again the important part of this error message is the part where the type of error is described:

ArgumentError:
       wrong number of arguments (1 for 0)

Now we have an ArgumentError. The test is trying to call our #introduction method with an argument (notice it says 1) but we haven't defined our method to take in any arguments, the for 0 part of the error message.

Let's fix that now:

# lib/introduction.rb

def introduction(name)
end

Run the test again and you'll see the following:

Failures:

  1) #introduction takes in an argument of a name and puts out a phrase with that name using string interpolation
     Failure/Error: expect{introduction("Josh")}.to output("Hi, my name is Josh.\n").to_stdout
       expected block to output "Hi, my name is Josh.\n" to stdout, but output nothing
       Diff:
       @@ -1,2 +1 @@
       -Hi, my name is Josh.
       
     # ./spec/introduction_spec.rb:5:in `block (2 levels) in <top (required)>'

Now the important part of our error message is here:

expected block to output "Hi, my name is Josh.\n" to stdout, but output nothing

Our test is expecting our method to puts out the exact phrase, using the value of the name argument that the method is called with.

Let's fix that:

# lib/introduction.rb

def introduction(name)
  puts "Hi, my name is #{name}."
end

Run the test again and we should be passing the first of our two tests. Use the test output and the procedure we just followed to get the second test passing.

The #introduction_with_language Method

Define a method, #introduction_with_language that takes in two arguments, name and language and outputs the phrase: `"Hi, my name is #{name} and I am learning to program in #{language}."

View Method Arguments Lab on Learn.co and start learning to code for free.

View Methods and Arguments Lab on Learn.co and start learning to code for free.

method-arguments-lab-v-000's People

Contributors

sophiedebenedetto avatar annjohn avatar amelieoller avatar

Watchers

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