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
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; |
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
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]); | |
} | |
} |
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 UnitConversion | |
def initialize(measurement, unit) | |
@measurement = measurement | |
@unit = unit | |
end | |
# Proxy with Kelvin | |
def to_kelvin | |
case @unit | |
when 'celcius' |
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
######### | |
# PROXY # | |
######### | |
def fahrenheit_to_kelvin(value) | |
(value + 459.67) * 5 / 9 | |
end | |
def kelvin_to_celcius(value) | |
value -= 273.15 |
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
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' } |
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
# == 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 | |
# |
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
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 |
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
# 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 |
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
# no "if" statement | |
def ternary(statement, true_result, false_result) | |
(true && statement) && true_result || false_result | |
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 split_message(message) | |
output_line = "" | |
message.each_line.with_index(1) do |line, index| | |
output_line << "Line #{index}: #{line}" | |
end | |
output_line | |
end |