forked from Code-the-Dream-School/Backend-ruby-methods
-
Notifications
You must be signed in to change notification settings - Fork 0
Week 4 - Methods lesson #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kristinamarkina
wants to merge
7
commits into
master
Choose a base branch
from
methods-lesson
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fb3796e
Create divisible.rb
kristinamarkina 597eb20
Create hangman.rb
kristinamarkina ea9b42a
Create hash_to_array.rb
kristinamarkina 882f642
Create sums.rb
kristinamarkina d99fa41
Create sort_blocks.rb
kristinamarkina 6daa62d
Update sort_blocks.rb
kristinamarkina e6286c5
Create block_function.rb
kristinamarkina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| def do_calc | ||
| yield 7, 9 | ||
| yield 7, 9 | ||
| end | ||
|
|
||
| sum = do_calc do |a,b| | ||
| a+b | ||
| end | ||
| puts "Sum of 7 and 9 is: #{sum}" | ||
|
|
||
| prod = do_calc do |a,b| | ||
| a*b | ||
| end | ||
| puts "Product of 7 and 9 is: #{prod}" | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| def divisible(n) | ||
| numbers = [] | ||
| number = 1 | ||
| while number < n | ||
| if number % 2 == 0 or number % 3 == 0 or number % 5 == 0 | ||
| numbers.push(number) | ||
| end | ||
| number += 1 | ||
| end | ||
| return numbers | ||
| end | ||
|
|
||
| n=100 | ||
|
|
||
| puts "All the numbers between 1 and #{n} that are divisible by 2 or 3 or 5: " | ||
| print divisible(n) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| def hangman(word, letters) | ||
| result = "" | ||
| word.each_char do |letter| | ||
| if letters.include?(letter) | ||
| result += letter | ||
| else | ||
| result += '_' | ||
| end | ||
| end | ||
| return result | ||
| end | ||
|
|
||
| word = "Xenotransplantation" | ||
| letters = ["a","b","c","d","e","X","o"] | ||
| puts hangman(word, letters) | ||
| puts hangman("bob", ["b"]) | ||
| puts hangman("alphabet",["a","h"]) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| def ask(question) | ||
| print question | ||
| gets.chomp | ||
| end | ||
|
|
||
| def print_hash(hash={"b" => 200}) | ||
| puts "Array of keys:" | ||
| puts hash.keys | ||
| puts "Array of values:" | ||
| puts hash.values | ||
| end | ||
|
|
||
| h=Hash.new | ||
| while h.size < 5 do | ||
| key = ask("Enter the key: ") | ||
| while h.has_key?(key) == true do | ||
| puts "Key #{key} is already in the hash." | ||
| key = ask "Enter different key: " | ||
| end | ||
| value = ask "Enter the value for key #{key}: " | ||
| h[key]=value | ||
| end | ||
|
|
||
| print_hash(h) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| class Book | ||
| attr_reader :author, :title, :count | ||
| def initialize(author,title,count) | ||
| @author = author | ||
| @title = title | ||
| @count = count | ||
| end | ||
| def to_s | ||
| "author: #{author} title: #{title} count: #{count}" | ||
| end | ||
| end | ||
|
|
||
| book_array = [] | ||
| book_array.push(Book.new("Beatrice Potter","Peter Rabbit",25)) | ||
| book_array.push(Book.new("Henry Fielding","Tom Jones",12)) | ||
| book_array.push(Book.new("Bob Woodward","All the President's Men",30)) | ||
|
|
||
| puts "\nSorting alphabetically by author" | ||
| new_array = book_array.sort do |a,b| | ||
| author1 = a.author.downcase | ||
| author2 = b.author.downcase | ||
| author1 <=> author2 | ||
| end | ||
| puts new_array | ||
|
|
||
| puts "\nSorting alphabetically by title" | ||
| new_array = book_array.sort do |a,b| | ||
| title1 = a.title.downcase | ||
| title2 = b.title.downcase | ||
| title1 <=> title2 | ||
| end | ||
| puts new_array | ||
|
|
||
| puts "\nSorting by copies" | ||
| new_array = book_array.sort do |a,b| | ||
| count1 = a.count | ||
| count2 = b.count | ||
| count1 <=> count2 | ||
| end | ||
| puts new_array |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| class Sum1 | ||
| attr_accessor:total | ||
|
|
||
| def initialize(num1, num2) | ||
| @total = num1 + num2 | ||
| end | ||
| end | ||
|
|
||
| class Sum2 | ||
|
|
||
| def initialize(a,b) | ||
| @a = a | ||
| @b = b | ||
| end | ||
|
|
||
| def new_total | ||
| @a+@b | ||
| end | ||
| end | ||
|
|
||
| sum_one = Sum1.new(5,6) | ||
| puts "Sum1 total result: ", sum_one.total | ||
| sum_two = Sum2.new(5,6) | ||
| puts "Sum2 new_total result: ", sum_two.new_total |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yieldkeyword 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.