Issuu on Google+

Full file at http://testbank360.eu/solution-manual-ruby-programming-1st-edition-jerry-lee-for

Chapter 2 Solutions Quick Quiz 1 1. What is the irb, and how is it started? Answer: irb stands for Interactive Ruby. It is a command line interface for interacting with Ruby. 2. List four commands that can be used to terminate an irb session. Answer: exit, quit, irb_exit and irb_quit. 3. What is an expression? Answer: An expression is a series of language keywords, operators, and variables that when executed, return a result. 4. What is the name of the term that describes the process used to create new objects? Answer: Instantiation. 5. What Screen class method writes blank lines to the console screen? Answer: cls

Quick Quiz 2 1. What is a structure that is used to store a collection of classes, methods, and constants? Answer: A module. 2. What does the phrase order of precedence mean? Answer: All programming languages process arithmetic operations according to a predefined set of rules referred to as the order of precedence. 3. Can the order of precedence be altered? How? Answer: Yes. The order of precedence can be altered by enclosing different parts of an arithmetic expression within parentheses.


Full file at http://testbank360.eu/solution-manual-ruby-programming-1st-edition-jerry-lee-for

4. What happens to the remainder part of an arithmetic equation in which one integer is divided by another integer? Answer: The remainder is automatically dropped. 5. Ruby supports thousands of methods that are included when it is installed on a computer. Access to documentation for these methods is provided via a tool referred to as __________. Answer: RDoc.

Comprehensive Check Questions 1. Every Ruby installation comes with a convenient and powerful tool called __________, which stands for Interactive Ruby. Answer: irb 2. The ___________ command displays a line of text on the screen and moves the cursor down to the beginning of the next line. Answer: print 3. Objects are: a. are created through a process known as instantiation. b. are created based on a predefined class definition. c. inherit all the features of the class upon which they are based. d. All of the above. Answer: d 4. Each individual IRB session maintains its own separate __________________. As a result, any classes, methods, or variables that are defined in one IRB session will not be visible or accessible in another IRB session. Answer: memory space 5. Which of the following math operators returns the remainder portion of a division operation? a. * b. ? c. ** d. % Answer: D


Full file at http://testbank360.eu/solution-manual-ruby-programming-1st-edition-jerry-lee-for

6. What is the name of the module that contains methods that support advanced mathematical operations? a. division b. math c. arithmetic d. IRB Answer: B 7. What command opens a new IRB session and displays IRB’s version information? Answer: irb -v 8. Arithmetic operations are performed based on a predetermined order of precedence. Which of the following choices correctly outline Ruby’s order of precedence? a. exponentiation > multiplication/division/modulus > addition/subtraction b. addition/subtraction > multiplication/division/modulus/exponentiation c. multiplication/division/modulus > exponentiation > addition/subtraction d. none of the above Answer: A 9. Access to on-line documentation for most Ruby’s methods is provided via a tool referred to as ______________? Answer: RDoc 10. (True/False) Ruby documentation can be accessed on your computer using ri, which is a command-line Ruby documentation viewer. Answer: True 11. In addition to using the exit command, you can close an IRB session using which of the following commands? A. quit B. irb_exit C. irb_quit D. all of the above Answer: D 12. Which of the following refers to a series of language keywords, operators, and variables that, when processed, returns a result?


Full file at http://testbank360.eu/solution-manual-ruby-programming-1st-edition-jerry-lee-for

A. statement B. comment C. expression D. none of the above Answer: C 13. (True/False) Unlike other programming languages, Ruby does not impose any syntactical rules upon the formulation of code statements. Answer: F 14. (True/False) While the IRB is effective at testing the execution of individual Ruby statements, it cannot be used to execute entire Ruby scripts. Answer: F 15. The term _____________ is used to describe the process whereby a new object is created. Answer: Instantiation 16. A _______________ is a structure used to store collections of classes, methods, and constants. Answer: Module 17. What characters can be used to override the order of precedence? A. ( and ) B. { and } C. # and # D. all of the above Answer: A 18. The round method rounds off a numerical value. Which of the following classes supports this method? a. float b. integer c. numeric d. all of the above Answer: D 19. After evaluating the following expression, what value would Ruby generate? 4 + 7 * 3 – 7 / 2 + 25


Full file at http://testbank360.eu/solution-manual-ruby-programming-1st-edition-jerry-lee-for

A. 27 B. 0 C. 47 D. none of the above Answer: C 20. (True/False) The ** (exponentiation) method is used to return the remainder portion of a division operation. Answer: F

Chapter Project Reinforcement Exercises The following exercises are designed to further the students understanding of Ruby programming by challenging them to make improvements to the Ruby Tall Tale game. 1. As currently designed, the Ruby Tall Tale game collects from the player five pieces of information that are used as input in the creation of the game’s story. As a result, there is a limit to how dynamic the story can be. Review the story, and look for opportunities to replace other keywords with user input. To implement this challenge, you will have to add programming logic that prompts the player for additional input and then incorporates that input into the story’s text via embedded variables. See file TallTale_ExerciseA.rb for an example script. See the highlights in file TallTale_ExerciseA.doc for a review of script modifications. 2. As a programmer, it is important to take credit for your hard work. Add a paragraph to the end of the story that provides the player with a little information about the game and its author. Add your URL as well. If you do not have a URL, use your school’s URL. To implement this modification, add a fifth paragraph to the end of the story and then add statements to the end of the script that display the paragraph and pause execution to give the player the chance to read it. See file TallTale_ExerciseB.rb for an example script. See the highlights in file TallTale_ExerciseB.doc for a review of script modifications. 3. Add a transitional message between the collection of story input and the telling of the story that lets the player know that input collection is done and that the game is ready to tell the story. To do so, you need to clear the console window and then display the transitional text.


Full file at http://testbank360.eu/solution-manual-ruby-programming-1st-edition-jerry-lee-for

See file TallTale_ExerciseC.rb for an example script. See the highlights in file TallTale_ExerciseC.doc for a review of script modifications. 4. Modify the program so that the message that’s displayed when the player elects not to play the game is also displayed at the end of the story. To do so, insert additional text messages using puts statements just before the else keyword in the Main Script Logic section. See file TallTale_ExerciseD.rb for an example script. See the highlights in file TallTale_ExerciseD.doc for a review of script modifications. 5. As currently written, the game prompts the player to enter n to quit and y to play. If n is entered, the game halts. However, if anything else is entered, or if the Enter key is pressed with nothing entered, the game resumes. Change the game so that it can only be played when the player enters y. Do this replacing the the first set of script statements that follows with the second set of script statements that follows. Replace this:      answer = STDIN.gets  #Collect the player's response      answer.chop!  #Remove any extra characters appended to the string

With this:      #Analyze the player's response if answer == "n" #See if the player elected not to play

with the statement provided below.      loop do

       #Execute the Screen object's cls method in order to clear the screen        Console_Screen.cls

       #Prompt the player for permission to begin the game        print "Would you like to hear a interesting story? (y/n)\n\n: "

       $answer = STDIN.gets  #Collect the player's response        $answer.chomp!  #Remove any extra characters appended to the string

       break if $answer == "y" || $answer == "n"


Full file at http://testbank360.eu/solution-manual-ruby-programming-1st-edition-jerry-lee-for      end

     #Analyze the player's response if $answer == "n" #See if the player elected not to play

See file TallTale_ExerciseE.rb for an example script. See the highlights in file TallTale_ExerciseE.doc for a review of script modifications.

Discovery Projects Project 2-1 Ruby is supported by an extensive collection of documentation made available via RDoc. Visit http://www.ruby-doc.org/ and become acquainted with the help and documentation made available through this community-driven site. To make sure you are equally conformable accessing documentation via the ri, use ri to display and review information about the following classes: 

Math

Numeric

String

Print off screen prints showing the output for each of these classes. Project 2-2 One of the most convenient ways of exchanging information about Ruby is through a mailing list. There are a number of them dedicated to Ruby. Sign up with one or more of these mailing lists, and take advantage of them as you work through the course. Two such mailing lists are ruby-talk and ruby-doc. To join ruby-talk, send an email to rubytalk-ctl@ruby-lang.org and enter the following line as the text message in the body portion of your email, replacing FirstName and LastName with your name: Subscribe FirstName LastName

To join ruby-doc, send an email to ruby-doc-ctl@ruby-lang.org and enter the following line as the text message in the body portion of your email, replacing FirstName and LastName with your name: Subscribe FirstName LastName

Print off screen prints when signing up for these two mailing lists. Project 2-3


Full file at http://testbank360.eu/solution-manual-ruby-programming-1st-edition-jerry-lee-for

The IRB command can be configurabled using the command line options shown in Table 2.1. Using the information provided there, perform the following tasks: 1. Configure and start a new IRB session that does not display a prompt. Solution:

irb --noprompt

2. Configure and start a new IRB session that displays a simple (>>) prompt. Solution:

irb --prompt simple

3. Execute an IRB command that displays IRB version information. Solution:

irb –version or irb –v Capture screen prints showing the output for each of these tasks.


Solution manual ruby programming 1st edition jerry lee ford