Week 4 - Methods lesson#1
Open
kristinamarkina wants to merge 7 commits into
Open
Conversation
(1) Write a method that returns in an array all the numbers between 1 and 100 that are divisible by 2 or 3 or 5. Then call the method in your program and print out what it returns. Call the program divisible.rb.
(2) Write a program hangman.rb that contains a function called hangman. The function's parameters are a word and an array of letters. It returns a string showing the letters that have been guessed. Call the function from within your program so that you know that it works.
Example: hangman("bob",["b"]) should evaluate to "b_b" Example: hangman("alphabet",["a","h"]) should return "a__ha___"
(3) Write a program that collects 5 keys and 5 values from the user, and stores them in a hash. Write a function that accepts the hash as optional parameters and prints out an array of keys and an array of values. Call the function within your program so you know it works. (Question: Can you find online information on Ruby hash methods that will help with this function?) Call this program hash_to_array.rb.
(4) Create a program sums.rb with two classes. (a) a class called Sum1 with an initialize method that takes two parameters and sets the instance variable total to the sum of the two. Include a line at the top of the class: attr_accessor :total (b) a class called Sum2 with an initialize method that takes two parameters a and b and sets the instance variable a to the parameter a and the instance variable b to the parameter b. Create a method new_total inside Sum2 that returns the sum of the instance variables a and b. (c) In the mainline program, create instances of Sum1 and Sum2, passing 5 and 6 as parameters on the new statement. Print out the total for Sum1. Print out the new_total for Sum2.
(5) The ruby array sort method can use a block to sort various arrays. The block must compare between two elements of the array, a and b. If a<b, it should evaluate to -1. If a==b, it should evaluate to 0. And if a>b, it should evaluate to 1. You should call program sort_blocks.rb. Add to this program. Add additional calls to book_array.sort that pass blocks. For your first call to sort, pass a block so that the array is sorted in order of title, and print out the array. For your second call to sort, pass a block so that the array is sorted in order of copies, and print out the array.
Add additional calls to book_array.sort that pass blocks. For your first call to sort, pass a block so that the array is sorted in order of title, and print out the array. For your second call to sort, pass a block so that the array is sorted in order of copies, and print out the array.
(6) Create a program block_function.rb. It should have a function do_calc that calls a block using a yield statement. The yield statement will pass the numbers 7 and 9 to a block, and then will print out the result. Call the do_calc function twice in your program. The first time, pass a block that adds the two numbers. The second time, pass a block that multiplies the two numbers. Your program should print out 16 and 63.
ZipDevil
approved these changes
Apr 26, 2021
ZipDevil
left a comment
There was a problem hiding this comment.
Great work! Many clean and compact code blocks.
Good understanding & usage of build-in methods, keep up the good works!
| @@ -0,0 +1,14 @@ | |||
| def do_calc | |||
| yield 7, 9 | |||
| yield 7, 9 | |||
There was a problem hiding this comment.
Yield keyword will run all code blocks in this file if no specific block/method assigned to it.
We only need one line of yield to pass 7 & 9 into the following methods (since we yield to TWO methods in the same file), so it won't print the same output twice when you call the file on the command line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Created 6 .rb files