Coder Social home page Coder Social logo

alexriosdev / programming-univbasics-3-methods-default-values-lab-hou01-seng-ft-100520 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-students/programming-univbasics-3-methods-default-values-lab-hou01-seng-ft-100520

0.0 0.0 0.0 19 KB

License: Other

Ruby 100.00%

programming-univbasics-3-methods-default-values-lab-hou01-seng-ft-100520's Introduction

Default Values: Meal Choice Lab

Learning Goals

  • Define a method that takes in required and optional arguments
  • Use puts to output a string
  • Define a return value

Introduction

We've demonstrated the basics of defining methods, printing output, and returning values. Let's combine the concepts we've shown you to create a "Meal Choice" in our Ruby program.

Instructions

We're attending a wedding and the newlyweds-to-be want to know what types of food they should provide at the banquet. Define a method named meal_choice that has three parameters:

  • Two required parameters for vegetables. Ex: veg1 and veg2
  • And an optional parameter for protein with a default value of tofu

This method will serve a couple of purposes - we want to output a two messages and return one of the messages as a string.

The first message will always be the same:

"What a nutritious meal!"

The second message is structured to include all three parameters like so:

"A plate of #{protein} with #{veg1} and #{veg2}."

If the meal_choice method was run with carrots, string beans, and lentils:

2.6.1 :001 > meal_choice('carrots', 'string beans', 'lentils')
What a nutritious meal!
A plate of lentils with carrots and string beans.
 => "A plate of lentils with carrots and string beans."

In addition, if we were to leave off the third parameter, lentils, we would expect the following:

2.6.1 :002 > meal_choice('carrots', 'string beans')
What a nutritious meal!
A plate of tofu with carrots and string beans.
 => "A plate of tofu with carrots and string beans."

Notice above that the return value shown on last line is the same message as what is output. You will need to compose this message in a way that you can output it and return it.

Write your solution in the file lib/meal_choice.rb using the information provided below.

Solving this Lab

Whether you're omnivorous, vegetarian, or vegan, you're going to be eating a nutritious meal!

First, we should build out the empty method with veg1 and veg2 as required parameters, and protein as an optional parameter with the default value of tofu:

def meal_choice(veg1, veg2, protein = 'tofu')

end

For the implementation, the first message is always the same, so we can handle that with a puts statement:

def meal_choice(veg1, veg2, protein = 'tofu')
  puts "What a nutritious meal!"
end

Now, for the second message, we could do something similar:

def meal_choice(veg1, veg2, protein = 'tofu')
  puts "What a nutritious meal!"
  puts "A plate of #{protein} with #{veg1} and #{veg2}."
end

With this, we've output all the messages and solved the first test in this lab. There is still something else needed, though - remember that puts statements return nil:

2.6.1 :003 > puts Hello!
Hello!
 => nil

Similarly, if we run the current version of meal_choice, we will get the following:

2.6.1 :001 > meal_choice('carrots', 'string beans', 'chicken')
What a nutritious meal!
A plate of chicken with carrots and string beans.
 => nil

Instead of ending the method with a puts, we will need to add an additional line specifying what to return. One possible solution:

def meal_choice(veg1, veg2, protein = 'tofu')
  puts "What a nutritious meal!"
  puts "A plate of #{protein} with #{veg1} and #{veg2}."
  "A plate of #{protein} with #{veg1} and #{veg2}."
end

This works, but this code is a bit repetitive. We are creating our custom message twice. Instead, we might consider assigning the message to a variable, outputting the variable, then returning it:

def meal_choice(veg1, veg2, protein = 'tofu')
  puts "What a nutritious meal!"
  meal = "A plate of #{protein} with #{veg1} and #{veg2}."
  puts meal
  meal
end

Ruby implicitly returns the value of the final expression, in this case, meal. We can include an explicit return:

def meal_choice(veg1, veg2, protein = 'tofu')
  puts "What a nutritious meal!"
  meal = "A plate of #{protein} with #{veg1} and #{veg2}."
  puts meal
  return meal
end

But it is not necessary here.

Conclusion

You're all set! You've successfully written a series of Ruby methods utilizing all of the various Ruby basics we've discuss thus far. Now, we'll teach you about scope, and how information can be passed around to different methods.

programming-univbasics-3-methods-default-values-lab-hou01-seng-ft-100520's People

Contributors

maxwellbenton avatar sgharms avatar alexriosdev 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.