Skip to content

Instantly share code, notes, and snippets.

VueJS offers two ways to display information conditionally with directives: v-if and v-show. v-if will only render to the DOM when its value evaluates to true. v-show will render to the DOM either way but will have inline styling to the effect of display: none; if its value evaluates to false.

v-if

<div class="root">
  <p v-if="true">Hello</p>
</div>

JavaScript web requests can be made in a number of ways - axios is a library that generally makes them fairly simple.

const axios = require("axios");

axios.get("http://middle-maps.herokuapp.com/api/locations/1008").then(function(response) {
  console.log(response.data);  // => {"name":"Bag End","lat":"34.15555","lng":"-118.1370833","type":"site","id":1008,"synonyms":[],"images":[],"reviews":[]}
});

Three common types of loops in JavaScript are for, forEach, and while loops. for loops are probably the most popular, although forEach loops have become more popular recently.

The examples of each below will use this fruits array as an example:

var fruits = ["apple", "grape", "mango", "banana"];

for loops

There are many different kinds of methods in ruby, including module methods, class methods, instance methods, and singleton methods.

module Animal
  def self.browse_petco # module method
    p "browsing petco"
  end
end

class Dog

When people talk about Big O, they're talking about time-efficiency. For an array of length n, how many steps does a given process take? Coefficients don't matter, nor do constants and lower-order terms.

O(2n) is just O(n)

O(n + 100) is just O(n)

O(n^2 + n) is just O(n^2)

O(n log n + log n) is just O(n log n)

In ruby, as in other programming languages, it's not always intuitive when you have access to what variables. Instance variables are available anywhere within a file (or other files that load or have access to that file). Non-instance variables, though, are sometimes available within code blocks and sometimes aren't. The table below is a quick reference, but we'll dive into specifics below.

Access Non-Instance Variables Defined Outside From Within Access Non-Instance Variables Defined Inside From Without
Methods no no
Loops yes no
Conditionals yes yes

Methods

In rails, the model methods find() and find_by() work in similar ways but have a few important differences. They can both be used on any model to search for a single instance of that model.

Movie.find_by(id: 1)  # => #<Movie id: 1, title: "fellowship of the ring: extended edition", runtime: 228, created_at: "2019-03-02 20:55:54", updated_at: "2019-03-02 20:55:54">

Movie.find(1)  # => #<Movie id: 1, title: "fellowship of the ring: extended edition", runtime: 228, created_at: "2019-03-02 20:55:54", updated_at: "2019-03-02 20:55:54">

A common use for either is in a RESTful show action

Developers learning ruby for the first time tend to learn puts before or around the same time as p. Both methods print to the terminal (or the server, if you're using rails or sinatra). puts seems like a nice counterpoint to gets, as in gets.chomp: gets gets information from the user, and puts puts it back on their screen. I'm inclined to disagree with this inclination strongly and say to never use puts (it's objectively worse), but there are cases where it's useful.

Times to use puts

If you're writing a program that runs in the terminal, puts can be useful. It's a more aesthetic way to print information and directions to the user. I could argue that anyone using the terminal to run your program is likely a developer and won't have any problem with seeing quotation marks, but I won't because I know I've been known to not give UI as much credence as it deserves.

Times to use p

In essentially any other case when you want something printed, use p. This is a great tool for debu

When looping in ruby, you have many options. It's common to see something like |variable_name| on the same line as the loop is defined: this is called a block variable. It's a variable we only have access to inside the loop, and it changes each time through the loop. We see this most often in an each loop.

fruits = ["apple", "pear", "grape", "mango"]

fruits.each do |fruit|
  p fruit
end

Different developers have different standards for and approaches to commit messages. In a broad sense, commit messages should be brief but descriptive.

Guidelines

  • Messages should be lowercase, except where refering to a class name or other object that is syntactically required to be capitalized
    • add a navbar to the homepage is better than Add a Navbar to the homepage
  • Messages should begin with a verb and have no subject
    • add a navbar to the homepage is better than I added a navbar to the homepage
  • Initial verbs should be present tense; I prefer command form, but normal present tense is also okay; avoid present progressive (-ing)
  • add a navbar to the homepage is better than added a navbar to the homepage