Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions block_function.rb
Original file line number Diff line number Diff line change
@@ -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

12 changes: 12 additions & 0 deletions divisible.rb
Original file line number Diff line number Diff line change
@@ -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(', ')
24 changes: 24 additions & 0 deletions hangman.rb
Original file line number Diff line number Diff line change
@@ -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}"
27 changes: 27 additions & 0 deletions hash_to_array.rb
Original file line number Diff line number Diff line change
@@ -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)

41 changes: 41 additions & 0 deletions sort_blocks.rb
Original file line number Diff line number Diff line change
@@ -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

29 changes: 29 additions & 0 deletions sums.rb
Original file line number Diff line number Diff line change
@@ -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}"