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
14 changes: 14 additions & 0 deletions block_function.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def do_calc
yield 7, 9
yield 7, 9
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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}"
16 changes: 16 additions & 0 deletions divisible.rb
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)
17 changes: 17 additions & 0 deletions hangman.rb
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"])
24 changes: 24 additions & 0 deletions hash_to_array.rb
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)
40 changes: 40 additions & 0 deletions sort_blocks.rb
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
24 changes: 24 additions & 0 deletions sums.rb
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