Skip to content

Instantly share code, notes, and snippets.

@trestles
Last active November 25, 2019 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trestles/08ac82e0ad5debbcdb2777eced4affde to your computer and use it in GitHub Desktop.
Save trestles/08ac82e0ad5debbcdb2777eced4affde to your computer and use it in GitHub Desktop.
internet brands

1. Voting

I created a users, referendums, questions, and answers tables in the code below. Based upon requirements, it sounds like you will be able to identify users - I'll assume that they are logged in via an auth mechanism. Also, I would suspect that you wouln't want a user to answer twice so I added an aswered_questions to user in light of not wanting to leak user information into answers. One could probably use a hashed value to identify users at - I'd probably need more info.

Re anomynity of answers, at small scale this could potentially be traced but I'm assuming that there are enough answers that, probabilistically, this wouldn't be achievable.

In the answers table, I changed the answer_value to a Bool. I suspect that there will be more than just true / false in the future but for time being, this is sufficient. There would probably need to be an answer_choices table or possibly convert the answer_value to an enum. However, an enum would not be 4th Normal Form (that's arguable though).

I've left out rails created_at / updated_at and not had fk constraits at db level.

CREATE TABLE users(
   id serial PRIMARY KEY,
   name VARCHAR (50) UNIQUE NOT NULL,
   answered_questions integer[] /* would allow us to know that this user has answered this specific question and not have two by same person*/
);

CREATE TABLE referendums(
   id serial PRIMARY KEY,
   name VARCHAR (50) UNIQUE NOT NULL,
);

CREATE TABLE questions(
   id serial PRIMARY KEY,
   name VARCHAR (50) UNIQUE NOT NULL,
   referendum_id integer NOT NULL
);
CREATE UNIQUE INDEX referendum_idx ON questions (referendum_id);
 
CREATE TABLE answers(
   id serial PRIMARY KEY,
   answer_value boolean NOT NULL
   question_id integer NOT NULL
);
CREATE UNIQUE INDEX question_idx ON answers (question_id);

2. Lies, damn lies, and git

How do you rewrite git history to make this happen? I'd do this as an interactive rebase. The syntax is a bit obscure so usually google it but:

git rebase --interactive 'COMMIT_OF_A^'

go to edit in rebase interface, make changes, commit, and then git rebase --continue.

This is outlined here: https://stackoverflow.com/a/1186549/152825

One caveat with this is whether you have pushed back to master already. If yes, you will need to force push which is generally considered bad practice but would satisfy the requirements of project. The risk is that somebody has forked from your-cool-branch.

Is B' the same hash as B? Why or why not? It's different. I believe that part of the hash creation is the source tree before which will now be different and hence a different hash is generated.

3. Ruby Metaprogramming

(a) What's Module.included? (b) What is a common (meta-)programming construct in which it is used? a. Callback for when a module is included into another class b. a reasonable example is in Devise for their validatable module (https://github.com/plataformatec/devise/blob/07f2712a22aa05b8da61c85307b80a3bd2ed6c4c/lib/devise/models/validatable.rb#L26 ). There is a devise method and you can include the validatable model allowing succinct and concise configuration.

The goal of included (and much Ruby metaprogramming) is to provide powerful tools to include outside code into your objects and allow maximum flexibility with that code. In that sense, it is very helpful especially in the more general-purpose aspects of code (ie frameworks and libraries). It can also certainly be used in userspace code too but should be careful not to overuse and limit the logic in it.

Some Sample code:

module SampleHello
 module ClassMethods
    def say_hello
      puts "hello!"
    end 
  end
  def self.included(base)
    puts "included by class: #{base}"
    base.extend(ClassMethods)
  end


  def say_hello
    puts "hello from instance method"
  end
end

class User
  include SampleHello
end

me = User.new 
User.say_hello
me.say_hello

4. We're All Scratching Our Heads About This One

This looks like it should just be has_many / belongs_to like the following and is also an n+1 query (use Bullit to identify)

class Post < ActiveRecord::Base
  has_many :comments 
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

You could eager load the comments like this: post = Post.includes(:comments).find(4356)

or just iterate through the comments. It will be two sql calls either way.

Re the time on production vs staging, I'll assume that there's a non-trivial amount of traffic on your production database instance. You might have hit a critical inflection point where you are saturating the connection pool (unlikely and can be investigatd using an APM tool) or there's some indexing / db load issue. The above N+1 query should be removed.

5. Unix tools

find /usr/local/ -type f -exec ls -1lrt "{}" +; >  grep -i 'aardvark' *

6. !important

What does the "!important" CSS declaration do? While I'm not the best front-end developer, CSS uses Cascading to manage the actually applied style. Normally, it's the most specific to the selector at hand. !important, when applied at a higher level style, forces sub-styles to not override the !important style.

Why do front-end developers suggest using it with caution? The paradigm of CSS is to allow it be cascaded and this breaks that pattern.

What are some cases in which it makes sense to use it? If you have a style-guide and don't want any divergence from it, I could see declaring things as important.

7. Javascript

This is a generator function and the output is:

> {value: 3, done: false}
> {value: 4, done: true}
> {value: undefined, done: true}

The yield statement causes it to stop and have a value of


{ 
  value: Any,
  done: true|false
} 

and done is achieved by the return statement. So once you hit value 4 and done = true, calling next is undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment