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..c7f4b44 --- /dev/null +++ b/divisible.rb @@ -0,0 +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 + 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(', ') diff --git a/hangman.rb b/hangman.rb new file mode 100644 index 0000000..48f4b40 --- /dev/null +++ b/hangman.rb @@ -0,0 +1,24 @@ +def hangman(word, guessed_letters) + display = "" + word.each_char do |char| + if guessed_letters.include?(char) + display += char + else + display += "_" + end + end + 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"] + + 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