Skip to content

Instantly share code, notes, and snippets.

View jamesyang124's full-sized avatar

James Yang jamesyang124

  • HTC
  • Richmond, California
View GitHub Profile

Ruby Week 2 Quiz

  1. Name what each of the below is:
a = 1    # => ex. a is a local variable, and is a Fixnum object with value 1
    @a = 2            # => @a is instance vairiable, Fixnum object.
    user = User.new   # => local instance, is an User object.
    user.name         # => instance getter method
    user.name = "Joe" # => instance setter method, set something to String object
  1. How does a class mixin a module?
  • By inculde a module, and module provides the instance method for that instance object.
  1. What's the difference between class variables and instance variables?

Rails Week 1 Quiz

  1. Why do they call it a relational database?
  • The database composite by tables and each table relate to other tables via the foreign keys. The tables composite by attribute column and data row to express a collection of data. By the way, primary key is for table itself quering.
  1. What is SQL?
  • Structural query language. A standard syntax to qurey and operate the database.
  1. There are two predominant views into a relational database. What are they, and how are they different?
  • Schema is one of the view to describe the data sctructre, data types, another view is the rows in each table to describe a value collection[row] of attributes[column], which is data view.
  • Data view describes data with its value and attribute name, Schema view desrcibes attributes' types and structure.

Rails Week 2 Quiz

  1. Name all the 7 (or 8) routes exposed by the resources keyword in the routes.rb file. Also name the 4 named routes, and how the request is routed to the controller/action.
  helpers         Http Verb   URI pattern       Controller#action
  posts_path      GET         /posts            post#index
  new_post_path   GET         /posts/new        post#new
  post_path       GET         /posts/:id        post#show
  edit_post_path  GET         /posts/:id/edit   post#edit
                  POST        /posts            post#create
PUT /posts/:id post#update

Rails Week 3 Quiz

  1. What's the difference between rendering and redirecting? What's the impact with regards to instance variables, view templates?
  • Render only refresh by templates, redirect_to will go the action controller methods. So the instance variables and params will reset by redirect_to, and render will not reset instance variable and params.
  1. If I need to display a message on the view template, and I'm redirecting, what's the easiest way to accomplish this?
  redirect_to @post, notice: 'message' 
@jamesyang124
jamesyang124 / ruby_meta.md
Last active April 5, 2024 10:50
Ruby meta programming

#!/bin/ruby --verion => 2.0.0-p353

Self

In Ruby, self is a special variable that always references the current object.

  • Inside class or module definition, self refer to the Class or Module object.
  • Inside instance method, self refer to future instance object.
  • Inside class method, self refer to the class.i
@jamesyang124
jamesyang124 / git_resources.md
Last active January 6, 2016 03:08
Review git commands again.

Process:

1. git clone git@github.com:rails/rails.git

2. git branch -b <new_branch>

3. git checkout <new_branch>

4. code work, add CHANGELOG.md
@jamesyang124
jamesyang124 / vagrant_osx_zsh_tricks.md
Last active December 18, 2015 16:10
Setup vagrant on OSX.
install virtualbox 4.3.12
install vagrant 1.6.3

vagrant add box_name /hashicorp/precise64
vagrant init box_name
vagrant up

//in Vagrantfile
config.vm.synced_folder "hots/directory", "geust_directory/app", type: "nfs"
@jamesyang124
jamesyang124 / ruby_style_guide_notes.md
Last active August 29, 2015 14:04
Ruby style guide notes

Ruby Style Guide Notes

https://github.com/JuanitoFatas/ruby-style-guide

  • Always run the Ruby interpreter with the -w option so it will warn you if you forget either of the rules below!
  • Use ||= freely to initialize variables.
# set name to Bozhidar, only if it's nil or false

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.