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
12 changes: 12 additions & 0 deletions block_function.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def do_calc
result = yield(7,9)
puts "#{result}"
end

do_calc do |a,b|
a + b
end

do_calc do |a,b|
a * b
end
17 changes: 17 additions & 0 deletions divisible.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Write a method that returns in an array all the numbers between 1 and 100 that are divisible by 2 or 3 or 5.
#Then call the method in your program and print out what it returns.
#Call the program divisible.rb.

def numbers
result = []
(1..100).each do |i|
if i % 2 == 0 || i % 3 == 0 || i % 5 == 0
puts result.push(i)
end
end
return result
end



puts numbers.inspect
26 changes: 26 additions & 0 deletions hangman.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Write a program hangman.rb that contains a function called hangman.
#The function's parameters are a word and an array of letters.
#It returns a string showing the letters that have been guessed.
#Call the function from within your program so that you know that it works.

#Example: hangman("bob",["b"]) should evaluate to "b_b"
#Example: hangman("alphabet",["a","h"]) should return "a__ha___"

#each do
#include?

def hangman(word, arr)
result = ""

word.each_char do |letters|
if arr.include?(letters)
result += letters
else
result += "_"
end
end
return result
end

puts hangman("bob",["b"])
puts hangman("alphabet",["a","h"])
21 changes: 21 additions & 0 deletions hash_to_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def keys_and_values(input_hash)
keys_array = input_hash.keys
values_array = input_hash.values

puts "Keys: #{keys_array}"
puts "Values: #{values_array}"
end

hash_pairs = {}

5.times do
print "Enter a key: "
key = gets.chomp

print "Enter a value: "
value = gets.chomp

hash_pairs[key] = value
end

keys_and_values(hash_pairs)
48 changes: 48 additions & 0 deletions sort_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 = book_array.sort do |a,b|
author1 = a.author.downcase
author2 = b.author.downcase
author1 <=> author2
# if author1 > author2
# 1
# elsif author1 < author2
# -1
# else
# 0
# end
end
puts new_array

puts "\n Sorting by Title"
new_array = book_array.sort do |a,b|
author1 = a.title.downcase
author2 = b.title.downcase
author1 <=> author2
end
puts new_array

puts "\n Sorting by Count"
new_array = book_array.sort do |a,b|
author1 = a.count
author2 = b.count
author1 <=> author2
end
puts new_array
25 changes: 25 additions & 0 deletions sums.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Sum1
attr_accessor :total

def initialize(i, j)
@total = i + j
end

end

class Sum2
def initialize(a, b)
@a = a
@b = b
end
def new_total
@a + @b
end
end

sum1 = Sum1.new(5, 6)
sum2 = Sum2.new(5, 6)

puts "Total for Sum1: #{sum1.total}"

puts "new_total for Sum2: #{sum2.new_total}"