Skip to content

Instantly share code, notes, and snippets.

@MrBean83
MrBean83 / gist:6646205
Created September 21, 2013 01:39
Sudoku (Elaine & Luis)
class Sudoku
def initialize(string)
string = string.split('')
@board = Array.new(9){Array.new(9)}
@board.each_index do |row|
@board[row].each_index do |column|
@board[row][column] = string.shift.to_i
if @board[row][column] == 0
class RomanNumerals
def initialize
self.re_pop
end
def converter(test_number, discard = 0, key = @roman_numbers.keys.first)
return "" if test_number == 0
(@roman_numbers[key] * (test_number / key)) +
converter(test_number % key, @roman_numbers.delete(key))
end
@MrBean83
MrBean83 / gist:6373407
Last active December 21, 2015 22:09
"Regular Expressions"
# Determine whether a string contains a Social Security number.
def has_ssn?(string)
/\d{3}-\d{2}-\d{4}/.match(string)
end
# Return the Social Security number from a string.
def grab_ssn(string)
def grab_ssn(string)
if /\d{3}-\d{2}-\d{4}/.match(string)
string.match(/\d{3}-\d{2}-\d{4}/).to_s
@MrBean83
MrBean83 / gist:6225555
Created August 13, 2013 20:51
"RPN Calculator"
class RPNCalculator
def evaluate(rpn)
rpn = rpn.split
array = rpn.inject([]) do |array, i|
if i.match(/\d+/)
array = i.to_i
else
eq = array.pop(2)
case
when i == "+"
@MrBean83
MrBean83 / gist:6201448
Last active December 20, 2015 21:59
"Class Warfare, Validate a Credit Card Number"
class CreditCard
def initialize(card_number)
@card = card_number.to_s
if @card.length != 16
raise ArgumentError.new("This is not a valid credit card number.")
end
end
def check_card
chars = @card.split(//)

Implementing a Reverse Polish notation calculator in Ruby

A couple weeks ago I had a go at implementing an RPN calculator in Ruby. I knew I wanted the calculator to function by popping operands out of an array populated with the values of the input expression, operating upon the operands with the appropriate operator, and pushing the result back into the stack of operands.

I was able to implement this in version 1, but it took forever and the resulting code was not very beautiful. Why?

  • I started coding before I had a thorough understanding of RPN

    Wait, 20 10 5 4 + * - is what now?

@MrBean83
MrBean83 / gist:6157398
Last active December 20, 2015 15:49
"Build a simple guessing game"
class GuessingGame
def initialize(answer)
@answer = answer
end
def guess(guess)
@guess = guess
result = nil
case guess
@MrBean83
MrBean83 / gist:6129399
Last active December 20, 2015 12:09
"Die Class 2: Arbitrary Symbols"
class Die
attr_accessor :labels
def initialize(labels)
unless labels.length > 0
raise ArgumentError.new("Where's your strings?")
end
@labels = labels
end
@MrBean83
MrBean83 / gist:6120024
Last active December 20, 2015 10:59
"Print out a times table"
def times_table(rows)
1.upto(rows).each do |line|
1.upto(rows).each do |num|
print "#{line*num} "
end
print "\n"
end
end
@MrBean83
MrBean83 / gist:6116094
Last active December 20, 2015 10:31
#FibonacciSequence
def is_fibonacci?(i)
n = i * Math.sqrt(5)
n1 = Math.sqrt((5*(i**2)+4))
n2 = Math.sqrt((5*(i**2)-4))
if (i == 1 || i == 0)
return true
elsif (n1 % 1 == 0) || (n2 % 1 == 0)
return true
else