From 0c0937984db8d11d74fcd5a695e2455c31428861 Mon Sep 17 00:00:00 2001 From: rojina pradhan Date: Mon, 23 Oct 2023 21:14:59 -0700 Subject: [PATCH 1/2] Ruby methods --- block_function.rb | 15 +++++++++++++++ divisible.rb | 13 +++++++++++++ hangman.rb | 21 +++++++++++++++++++++ hash_to_array.rb | 27 +++++++++++++++++++++++++++ sort_blocks.rb | 41 +++++++++++++++++++++++++++++++++++++++++ sums.rb | 29 +++++++++++++++++++++++++++++ 6 files changed, 146 insertions(+) create mode 100644 block_function.rb create mode 100644 divisible.rb create mode 100644 hangman.rb create mode 100644 hash_to_array.rb create mode 100644 sort_blocks.rb create mode 100644 sums.rb diff --git a/block_function.rb b/block_function.rb new file mode 100644 index 0000000..bc290d6 --- /dev/null +++ b/block_function.rb @@ -0,0 +1,15 @@ +def do_calc + result = yield(7, 9) + puts result + end + + # Call do_calc with a block for addition + do_calc do |a, b| + a + b + end + + # Call do_calc with a block for multiplication + do_calc do |a, b| + a * b + end + \ No newline at end of file diff --git a/divisible.rb b/divisible.rb new file mode 100644 index 0000000..44ba490 --- /dev/null +++ b/divisible.rb @@ -0,0 +1,13 @@ + + divisible_numbers = [] + (1..100).each do |number| + if number % 2 == 0 || number % 3 == 0 || number % 5 == 0 + divisible_numbers << number + end + end + return divisible_numbers + + divisible_numbers = find_divisible_numbers + puts "Numbers between 1 and 100 that are divisible by 2, 3, or 5:" + puts divisible_numbers.join(', ') + \ No newline at end of file diff --git a/hangman.rb b/hangman.rb new file mode 100644 index 0000000..b02d18a --- /dev/null +++ b/hangman.rb @@ -0,0 +1,21 @@ +def hangman(word, guessed_letters) + display = "" + word.each_char do |char| + if guessed_letters.include?(char) + display += char + else + display += "_" + end + end + return display + end + + # Test the hangman function + word_to_guess = "hangman" + guessed_letters = ["a", "n", "g"] + + word_to_guess = "coding" + guessed_letters = ["o", "d", "g"] + + result = hangman(word_to_guess, guessed_letters) + puts "Word: #{result}" diff --git a/hash_to_array.rb b/hash_to_array.rb new file mode 100644 index 0000000..7c79e57 --- /dev/null +++ b/hash_to_array.rb @@ -0,0 +1,27 @@ +# Function to collect keys and values from the user and create a hash +def collect_keys_and_values + hash = {} + 5.times do + print "Enter a key: " + key = gets.chomp + print "Enter a value: " + value = gets.chomp + hash[key] = value + end + return hash + end + + # Function to print keys and values from a hash as arrays + def print_hash_as_arrays(input_hash) + keys_array = input_hash.keys + values_array = input_hash.values + puts "Keys: #{keys_array.join(', ')}" + puts "Values: #{values_array.join(', ')}" + end + + # Collect keys and values from the user + user_hash = collect_keys_and_values + + # Call the function to print keys and values as arrays + print_hash_as_arrays(user_hash) + \ No newline at end of file diff --git a/sort_blocks.rb b/sort_blocks.rb new file mode 100644 index 0000000..96b59a3 --- /dev/null +++ b/sort_blocks.rb @@ -0,0 +1,41 @@ +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 "Sorting alphabetically by author" + new_array_author = book_array.sort do |a, b| + author1 = a.author.downcase + author2 = b.author.downcase + author1 <=> author2 + end + puts new_array_author + + puts "Sorting alphabetically by title" + new_array_title = book_array.sort do |a, b| + title1 = a.title.downcase + title2 = b.title.downcase + title1 <=> title2 + end + puts new_array_title + + puts "Sorting by number of copies" + new_array_copies = book_array.sort do |a, b| + a.count <=> b.count + end + puts new_array_copies + \ No newline at end of file diff --git a/sums.rb b/sums.rb new file mode 100644 index 0000000..cb727f2 --- /dev/null +++ b/sums.rb @@ -0,0 +1,29 @@ +class Sum1 + attr_accessor :total + + def initialize(param1, param2) + @total = param1 + param2 + end + end + + class Sum2 + def initialize(a, b) + @a = a + @b = b + end + + def new_total + @a + @b + end + end + + # Create instances of Sum1 and Sum2 + sum1_instance = Sum1.new(5, 6) + sum2_instance = Sum2.new(5, 6) + + # Print out the total for Sum1 + puts "Total for Sum1: #{sum1_instance.total}" + + # Print out the new_total for Sum2 + puts "New Total for Sum2: #{sum2_instance.new_total}" + \ No newline at end of file From 1badd0eb944930b7b28ba621d320178440642182 Mon Sep 17 00:00:00 2001 From: rojina pradhan Date: Fri, 10 Nov 2023 13:18:18 -0800 Subject: [PATCH 2/2] Errors fixed for methods-lesson --- divisible.rb | 7 +++---- hangman.rb | 7 +++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/divisible.rb b/divisible.rb index 44ba490..c7f4b44 100644 --- a/divisible.rb +++ b/divisible.rb @@ -1,13 +1,12 @@ - +def find_divisible_numbers divisible_numbers = [] (1..100).each do |number| if number % 2 == 0 || number % 3 == 0 || number % 5 == 0 divisible_numbers << number end end - return divisible_numbers - + divisible_numbers + end divisible_numbers = find_divisible_numbers puts "Numbers between 1 and 100 that are divisible by 2, 3, or 5:" puts divisible_numbers.join(', ') - \ No newline at end of file diff --git a/hangman.rb b/hangman.rb index b02d18a..48f4b40 100644 --- a/hangman.rb +++ b/hangman.rb @@ -7,13 +7,16 @@ def hangman(word, guessed_letters) display += "_" end end - return display + display end # Test the hangman function word_to_guess = "hangman" guessed_letters = ["a", "n", "g"] - + + result = hangman(word_to_guess, guessed_letters) + puts "Word: #{result}" + word_to_guess = "coding" guessed_letters = ["o", "d", "g"]