This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SumOfMultiples | |
| attr_reader :multiples_of | |
| def self.to(max) | |
| SumOfMultiples.new(3, 5).to(max) | |
| end | |
| def initialize(*multiples_of) | |
| @multiples_of = multiples_of | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Trinary | |
| def initialize(num) | |
| num = '0' unless num =~ /^[012]+$/ | |
| @num = num.reverse.chars.collect(&:to_i) | |
| end | |
| def to_decimal | |
| @num.map.with_index { |num, idx| num * (3 ** idx) }.inject(:+) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Clock | |
| def self.at(hour, min = 0) | |
| ClockTime.new(hour, min) | |
| end | |
| end | |
| class ClockTime | |
| attr_accessor :time | |
| def initialize(hour, min) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class School | |
| def initialize | |
| @school = Hash.new { |school, grade| school[grade] = [] } | |
| end | |
| def to_h | |
| @school.sort.map { |grade, students| [grade, students.sort] }.to_h | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PhoneNumber | |
| attr_reader :number | |
| def initialize(number) | |
| @number = authenticate(number) | |
| end | |
| def to_s | |
| "(#{area_code}) #{number[3..5]}-#{number[6..9]}" | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CircularBuffer | |
| class BufferEmptyException < StandardError; end | |
| class BufferFullException < StandardError; end | |
| attr_reader :buffer, :buffer_size | |
| attr_accessor :read_id, :write_id | |
| def initialize(buffer_size) | |
| @buffer = [] | |
| @buffer_size = buffer_size |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Robot | |
| attr_accessor :bearing, :coordinates | |
| def orient(direction) | |
| if [:north, :east, :south, :west].include? direction | |
| self.bearing = direction | |
| else | |
| raise ArgumentError, "Invalid direction" | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Queens | |
| attr_reader :white, :black | |
| def initialize(queens = { white: [0, 3], black: [7, 3] }) | |
| fail ArgumentError if queens[:white] == queens[:black] | |
| @white = queens[:white] | |
| @black = queens[:black] | |
| end | |
| def to_s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Squares | |
| attr_reader :max | |
| def initialize(max) | |
| @max = max | |
| end | |
| def square_of_sums | |
| (1..max).inject(:+) ** 2 | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PigLatin | |
| def self.translate(phrase) | |
| pig = PigLatin.new(phrase).convert | |
| end | |
| attr_reader :words | |
| def initialize(phrase) | |
| @words = phrase.split | |
| end |
NewerOlder