Skip to content

Instantly share code, notes, and snippets.

View Mk-Etlinger's full-sized avatar

Mike Etlinger Mk-Etlinger

  • Grand Rapids
View GitHub Profile
@Mk-Etlinger
Mk-Etlinger / sorcery.rb
Last active February 5, 2020 01:45
Cleaned up meta programming set_resorcery method
class ApplicationController < ActionController::API
def metaprogram_controller_instance_variable
model_name = params['controller'].singularize
Model = Object.const_get(model_name.capitalize)
value_to_set = Model.find_by(id: params[:id])
instance_variable_set('@' + model_name, value_to_set)
end
end
@Mk-Etlinger
Mk-Etlinger / gist:9a17dea7005f695df6493f37823d305c
Created February 22, 2018 06:11
Metaprogram setting your resource as an instance variable
class ApplicationController < ActionController::API
before_action :authenticate_user
def sorcery
controller_name = params['controller']
controller_name.chop!
model_name = controller_name.capitalize
instance_variable_set('@' + controller_name, Object.const_get( model_name ).find_by(id: params[:id]))
end
end
@Mk-Etlinger
Mk-Etlinger / gist:252495d837b0f92444eb3f69ae1bbb0a
Created February 22, 2018 05:38
First attempt at metaprogramming set_instance_variable method
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
request.env['omniauth.origin'] || dashboard_path
end
def index
render "landing/index"
end
class User
attr_accessor :name, :user_name, :age, :location, :bio
def initialize(attributes)
attributes.each {|key, value| self.send(("#{key}="), value)}
end
end
def sorcery
controller_name = params['controller']
controller_name.chop!
model_name = controller_name.capitalize
instance_variable_set('@' + controller_name, Object.const_get( model_name ).find_by(id: params[:id]))
end
const myWorkSchedule = {
monday: '10-5',
tuesday: '10-5',
friday: '11-9'
}
const updatedSchedule = { thursday: '11-6', ...myWorkSchedule }
updatedSchedule
// {thursday: "11-6", monday: "10-5", tuesday: "10-5", friday: "11-9"}
@Mk-Etlinger
Mk-Etlinger / gist:1ff14785f30460f9f3fde24149e7c7cd
Created February 8, 2018 04:05
Object.assign syntax usage
const myWorkSchedule = {
monday: '10-5',
tuesday: '10-5',
friday: '11-9'
}
const updatedSchedule = Object.assign({}, myWorkSchedule, { thursday: '8-5' })
updatedSchedule
// {monday: "10-5", tuesday: "10-5", friday: "11-9", thursday: "8-5"}
var twoSum = function(nums, target) {
let dictionary = {}
let complementValue = 0;
let indicesArray = [];
for (let i = 0; i < nums.length; i++) {
complementValue = target - nums[i];
if (dictionary.hasOwnProperty( complementValue )) {
return indicesArray = [ dictionary[complementValue], i ]
}
var twoSum = function(nums, target) {
let indicesArray = [];
for (let i = 0; i < nums.length; i++) {
for (let k = i + 1; k < nums.length; k++) {
if (nums[i] + nums[k] === target) {
indicesArray.push(i, k)
}
}
}
return indicesArray;
// Input: [ 2, 7, 11, 15 ]
//* Use a nested for loop
//* compare the current num(i) to the next num(k++) and check if they match
//* if they match then return both indices in an array
// Output: [ 0, 1 ]
// edge cases: ??