Skip to content

Instantly share code, notes, and snippets.

@mayfer
mayfer / exam.rb
Created February 6, 2014 00:53
Example ruby exercise
class Exam
attr_accessor :passes
attr_accessor :fails
def initialize(students)
@students = students
@passes = []
@mayfer
mayfer / anagrams.rb
Created February 20, 2014 21:41
Find the word with most number of anagrams
words = File.open('/usr/share/dict/words').read
words.gsub!(/\r\n?/, "")
hashes = {}
anagrams = {}
most = 1
most_anagram = ''
words.each_line do |word|
word = word.strip.downcase
@mayfer
mayfer / algorithms.rb
Created February 21, 2014 20:29
Problem solving and runtime complexity exercises
# given an array of integers, find the largest value
def find_largest(numbers)
# initialize our "largest number" to the first number in the array
# because it is the largest so far
largest = numbers[0]
# looping over each number once
numbers.each do |i|
@mayfer
mayfer / orm.rb
Created May 21, 2014 18:00
Generic ORM example
require 'pg'
class Orm
@@conn = PG.connect(
dbname: 'dfq35t7uirbums',
port: 5432,
user: 'bmdjwluxchptuq',
host: 'ec2-54-204-41-178.compute-1.amazonaws.com',
password: 'aEH-cKdr2zoXYUAjI8Xjma5eXK'
@mayfer
mayfer / author.rb
Created May 21, 2014 18:01
Author ORM example
require 'pg'
class Author
@@conn = PG.connect(
dbname: 'dfq35t7uirbums',
port: 5432,
user: 'bmdjwluxchptuq',
host: 'ec2-54-204-41-178.compute-1.amazonaws.com',
password: 'aEH-cKdr2zoXYUAjI8Xjma5eXK'
@mayfer
mayfer / examples.rb
Created June 3, 2014 23:55
Functions that find most common prey, and see if words rhyme
r = {
"shrimp" => [
"algae",
"other_shrimps",
"seaweed",
],
"toad" => [
"children",
"flies",
@mayfer
mayfer / input.rb
Created July 9, 2014 00:20
User input example
puts "Enter A"
a = gets.chomp
puts "Enter B"
b = gets.chomp
c = Integer(a) + Integer(b)
puts c
@mayfer
mayfer / rent.rb
Created July 9, 2014 00:20
Rent testing example
# must be baller and either furnished or rent cheaper than 2100
def rent?(furnished, rent, baller)
if baller && furnished || rent < 2100
return true
else
return false
end
end
@mayfer
mayfer / sound.js
Created July 11, 2014 00:32
Basic audio generation
// Built from Mohit Cheppudira's sine wave generator - http://0xfe.blogspot.com
// Modified by Murat Ayfer - http://muratayfer.com
soundWave = function(context) {
// xs is a list of x (time) values, one per wave.
// time is not represented as synchronized clicks or milliseconds, its passing is freq dependent
// so that's why we keep a value per each wave.
this.xs = [];
this.counter = 0;
this.context = context;
@mayfer
mayfer / orm.rb
Created July 23, 2014 00:42
ORM breakout
class ORM
def save
table_name = self.class
sql_columns = self.instance_variables.map do |i|
i.slice(1, i.length)
end.join(', ')
sql_values = self.instance_variables.map do |i|
'"' << self.instance_variable_get("#{i}") << '"'