Skip to content

Instantly share code, notes, and snippets.

@eternal44
eternal44 / transform.js
Created December 9, 2015 02:16
transform (map) function
var transform = function(collection, predicate){
if (Array.isArray(collection)){
return collection.map(predicate);
} else if (typeof collection === 'string'){
collection = collection.split('');
return collection.map(predicate).join('');
} else if (typeof collection === 'object'){
return Object.keys(collection).reduce(function(results, property){
results[property] = predicate(collection[property]);
return results;
@eternal44
eternal44 / elseif.js
Created December 4, 2015 00:07
else / else if review
function each(collection, predicate){
if (Array.isArray(collection) || typeof collection === 'string'){
for (var i = 0; i < collection.length; i++){
predicate(collection[i]);
}
} else if(typeof collection === 'object') {
for (var j in collection){
predicate(collection[j]);
}
}
@eternal44
eternal44 / temperature_conversions.rb
Created October 6, 2015 06:13
No argument in conversion methods
class UnitConversion
def initialize(measurement, unit)
@measurement = measurement
@unit = unit
end
# Proxy with Kelvin
def to_kelvin
case @unit
when 'celcius'
#########
# PROXY #
#########
def fahrenheit_to_kelvin(value)
(value + 459.67) * 5 / 9
end
def kelvin_to_celcius(value)
value -= 273.15
require 'test_helper'
class RoutesTest < ActionDispatch::IntegrationTest
test 'should create new post' do
assert_routing({ path: 'posts', method: :post }, { controller: 'posts',
action: 'create' })
end
test 'should get post' do
assert_routing '/posts/1', { controller: 'posts', action: 'show', id: '1' }
@eternal44
eternal44 / app_models_friendship.rb
Last active September 11, 2015 17:58
lity.herokuapp.com - complex associations
# == Schema Information
#
# Table name: friendships
#
# id :integer not null, primary key
# user_id :integer
# friend_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
@eternal44
eternal44 / app_helpers_group_helper.rb
Last active August 26, 2015 18:05
lity.herokuapp.com - complex authorizations
module GroupHelper
def admin(group)
true if group.memberships.admin.where(user_id: current_user.id).present?
end
def regular(group)
true if group.memberships.member.where(user_id: current_user.id).present?
end
end
@eternal44
eternal44 / 3c_column-string_alignment.rb
Created August 23, 2015 23:12
Refactor challenge to use correct input & output layout
# prints line numbers before each line & aligns the ":"
class ListGenerator
def column_alignment(lines)
lines = lines.split("\n")
lines.map.with_index(1) do |output, line_number|
"Line #{number_spacing(lines, line_number)}#{line_number}: "\
"#{output}"
end
@eternal44
eternal44 / 2c_formal_logic_challenge.rb
Last active August 29, 2015 14:28
Alternative ternary statement - without "if" statement
# no "if" statement
def ternary(statement, true_result, false_result)
(true && statement) && true_result || false_result
end
@eternal44
eternal44 / 3c_column-string_alignment.rb
Created August 23, 2015 20:41
Comparing "each_line" with "collect"
def split_message(message)
output_line = ""
message.each_line.with_index(1) do |line, index|
output_line << "Line #{index}: #{line}"
end
output_line
end