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
class User
attr_accessor :name, :user_name, :age, :location, :bio
def initialize(attributes)
attributes.each {|key, value| self.send(("#{key}="), value)}
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
@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 / 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

Implement an Expression Evaluator Our goal is to write a program that is capable of evaluating simple mathematical expressions. An example expression could look like:

+ 1 2 This expression can be read as 1 + 2 and evaluates to 3. We've placed the operator (+) first in our syntax to simplify some things down the road. The + indicates that this is an operator expression, and is evaluated by summing the two following tokens, which in this case are both numbers.

The general rules are:

Expressions can be numbers or operator expressions (4 is an expression and so is - 3 1) Numbers evaluate to themselves (4 is 4)

def self.parse(string)
sum = 0
char_array = string.split
char_array.each_with_index do |char, i|
sum += char.to_i
next unless char == '-' && char_array[i + 2]
if char_array[i + 2].to_i == 0
char_array[i + 1] = char + char_array[i + 1]
else
char_array[i + 2] = char + char_array[i + 2]
class ExpressionEvaluator
def self.parse(string)
complex_exps = string.scan(/-\s\d+\s\d+/)
simple_exps = string.gsub(/-\s\d+\s\d+/, '')
complex_sum(complex_exps) + simple_sum(simple_exps)
end
end
class ExpressionEvaluator
def self.parse(string)
complex_exps = string.scan(/-\s\d+\s\d+/)
simple_exps = string.gsub(/-\s\d+\s\d+/, '')
complex_sum(complex_exps) + simple_sum(simple_exps)
end
def self.complex_sum(array)
array.reduce(0) { |sum, exp| sum + (exp.split[1].to_i - exp.split[2].to_i) }
end
@Mk-Etlinger
Mk-Etlinger / setup.php
Created November 20, 2018 17:49
From setup.php
add_action('init', function(){
session_set_cookie_params( 0 );
if(!session_id())
session_start();
}, 1);
@Mk-Etlinger
Mk-Etlinger / README.md
Created June 7, 2019 15:11 — forked from benjaminbarbe/README.md
Nginx proxy to S3 with caching