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
# There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on. | |
# There are 64 squares on a chessboard. | |
# Write a program that shows | |
# - how many grains were on each square, and | |
# - the total number of grains | |
# ## For bonus points |
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
RSpec.configure do |config| | |
# Use color in STDOUT | |
config.color_enabled = true | |
# Use color not only in STDOUT but also in pagers and files | |
config.tty = true | |
# Use the specified formatter | |
config.formatter = :progress # :progress, :html, :textmate | |
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
def prime_slow?(number) | |
(2..number/2).each {|num| return false if number % num == 0} | |
true | |
end | |
puts prime_slow?(19) | |
puts prime_slow?(14) | |
puts prime_slow?(157) | |
puts prime_slow?(273) |
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
filenames = [ | |
"foo-1.10.2.ext", | |
"foo-1.11.ext", | |
"foo-1.3.ext", | |
"foo-1.50.ext", | |
"foo-1.8.7.ext", | |
"foo-1.9.3.ext", | |
"foo-1.ext", | |
"foo-10.1.ext", | |
"foo-10.ext", |
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
# Download this file: | |
# https://gist.github.com/aviflombaum/28534c5d69edd2439a7d/download | |
# Run it from your terminal with: | |
# ruby ruby.basics.rb | |
# (Just make sure you are in the right directory) | |
# ====================================== | |
# Ignore All This Code | |
# ====================================== |
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
# # Roman Numerals | |
# The Romans were a clever bunch. They conquered most of Europe and | |
# ruled it for hundreds of years. They invented concrete and straight | |
# roads and even bikinis. One thing they never discovered though was | |
# the number zero. This made writing and dating extensive histories | |
# of their exploits slightly more challenging, but the system of | |
# numbers they came up with is still in use today. For example the | |
# BBC uses Roman numerals to date their programmes. |
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 Person | |
def initialize(attribute_hash) | |
@attr_hash = attribute_hash | |
@attr_hash.each do |attribute, value| | |
self.metaclass.send(:define_method, "#{attribute}") do | |
@attr_hash[attribute] | |
end | |
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 Triangle | |
attr_accessor :kind | |
def initialize(a, b, c) | |
raise TriangleError if a<=0 || b<=0 || c<=0 | |
raise TriangleError if a+b<=c || b+c<=a || a+c<=b | |
if a == b && b == c | |
self.kind = :equilateral | |
elsif a == b || b == c || c == a |
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
# A Jukebox filled with songs | |
# that responds to help, list, play, and exit | |
require_relative 'song' | |
class Jukebox | |
attr_accessor :songs | |
APPROVED_COMMANDS = [:list, :help, :exit] | |
def initialize(songs) | |
@songs = songs |
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 | |
attr_reader :name, :roster | |
def initialize(name) | |
@roster = {} | |
@name = name | |
end | |
def add_student(name, grade) |
NewerOlder