diff --git a/block_function.rb b/block_function.rb new file mode 100644 index 0000000..6473611 --- /dev/null +++ b/block_function.rb @@ -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 \ No newline at end of file diff --git a/divisible.rb b/divisible.rb new file mode 100644 index 0000000..ff912b7 --- /dev/null +++ b/divisible.rb @@ -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 \ No newline at end of file diff --git a/hangman.rb b/hangman.rb new file mode 100644 index 0000000..92c751b --- /dev/null +++ b/hangman.rb @@ -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"]) \ No newline at end of file diff --git a/hash_to_array.rb b/hash_to_array.rb new file mode 100644 index 0000000..bdd88f4 --- /dev/null +++ b/hash_to_array.rb @@ -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) diff --git a/sort_blocks.rb b/sort_blocks.rb new file mode 100644 index 0000000..42ed8c5 --- /dev/null +++ b/sort_blocks.rb @@ -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 \ No newline at end of file diff --git a/sums.rb b/sums.rb new file mode 100644 index 0000000..79ae6a0 --- /dev/null +++ b/sums.rb @@ -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}" \ No newline at end of file