panicbus
12/7/2013 - 6:11 PM

answers.md

Test

Misc

  1. Explain how APIs work. In detail.
    • Definition - An Application Programming Interface; defines how to interact with some software service.
    • How you interact with them - On the web, you make HTTP requests. For a gem, you might call methods.
    • Why/when you need to use them - When interacting with an application or library you don't own.
    • When you might build one yourself - When you want to expose functionality of your app/library to other developers.
  2. What is the purpose of yield? (Ruby) Show an example. - [1]
    • yield calls the block which was passed into a method. The parameters to yield will be passed to the block.
    • What Javascript concept is the analogous to? - Callbacks
  3. Construct a regular expression that matches a valid email address.
  4. Explain how http works in detail.
    • Browsers open connections to HTTP servers and issue requests.
    • Requests contain a verb (GET/POST/PUT/PATCH/DELETE/OPTIONS) and a resource ('/things')
    • Requests can contain headers and a body
    • The server sense a response in exchange
    • The response has headers and a body as wel
  5. Write a method that appends the words “in Ruby” to any English sentence, in Ruby. [2]
  6. Write a method that converts x amount of dollars into change and specify how much of each coin will you have. E.g. convert $2.65 and return the amount of quarters, dimes, nickels, and pennies. [3]
    • Use a regular expression to extract the dollars and cents from the string
    • You may use http://rubular.com
  7. What's the difference between ActiveRecord callbacks and validations?
    • Callbacks are used to call your own code based on certain events like save, create, destroy, etc
    • Validations are a DSL used to describe conditions on record attributes, and although they are integrated into the callback system they are not themselves callbacks.
  8. In Javascript, what's the difference between null and undefined?
    • A variable that has never been set is undefined, a variable that exists with no value is null
  9. When would you use a CSS float?
    • Floats are used to push elements to one side or the other without content before/after.
  10. What is the "Box Model" in CSS? Which CSS properties are a part of it?
    • The box model is how CSS lays out elements. All elements are considered to be in a box; boxes are affected by margin, padding, border, and the size of the content

[1] Example of yield

def yield_param_to_block(param)
  yield param
end

yield_param_to_block("Hello, world!") do |yieled_param|
  puts yieled_param
end

[2] Append the words "in Ruby" to sentence

def append_in_ruby_to(sentence)
  "#{sentence} in Ruby"
end

[3] Convert to coins

def convert(str)
  str.match /^\$([0-9]+)\.([0-9]{1,2})$/ do |m|
    dollars = m[1].to_i
    cents   = m[2].to_i
    out     = {}

    # All dollars become quarters
    out[:quarters] = dollars * 4

    # Use integer division to find out how to make change
    out[:quarters] += cents / 25

    # Modulo to see how many cents are left after taking out the quarters
    cents = cents % 25

    # Do dimes
    out[:dimes] = cents / 10
    cents = cents % 10

    # And nickels (of which there can/may only be one)
    out[:nickels] = cents / 5

    # Anything left is pennies
    out[:pennies] = cents % 5

    # Don't forget to return
    out
  end
end