Skip to content

Instantly share code, notes, and snippets.

Array

::new
*
+
-
<=>
==
<<

Hash

==
[key]
[key]=
any?
any? {|key, value| ...}
delete(key)
delete(key) {|key| ..}

String

::new
*
+
<<
<=>
==
=~

Ruby and General Programming:

  • What is a class? What is an object? What are the differences in Ruby?

  • Are instance variables available to class methods?

  • What are the differences between blocks, procs and lambdas in Ruby?

  • Write a method that sorts the keys in a hash by the length of the key as a string. For instance, the hash:

    { abc: 'hello', 'another_key' => 123, 4567 => 'third' }
class MySet
end
require 'minitest/autorun'
class TestMySet < Minitest::Test
def test_empty_set_should_have_size_0
end
def test_initialize_with_an_array
end
local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ %s)"
PROMPT='${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$reset_color%}$(git_prompt_info)% %{$reset_color%}'
git_prompt_info () {
local g="$(git rev-parse --git-dir 2>/dev/null)"
if [ -n "$g" ]; then
local r
local b
local d
local s
Simplest Rails
1. Route, ApplicationsController that renders some HTML
2. Separate content and flow control - HTML as "views"
3. dynamically generated pages
- data with different time
- render template, passing in the data
- render "dir/hello_world.html", locals: {time: "am"}
- data from the URL
require 'rack'
require './extract_data'
surfing_app = Surfing.new
run surfing_app
at_exit do
data = YAML.dump(surfing_app.journal_entries)
File.open("data/journal_entries.yml", 'w') do |file|
file.write(data)
@knwang
knwang / user_invite_friend_spec.rb
Created June 14, 2013 07:41
Rspec feature spec with rspec-given
require 'spec_helper'
feature 'User invites a friend' do
context 'User successfully invites friend to join' do
Given(:alice) { Fabricate(:user) }
When do
sign_in(alice)
invite_a_friend
friend_accepts_invitation
@knwang
knwang / gist:5020668
Last active December 14, 2015 03:28
How Does a Web Application Keep Track of User Identity?

How Does a Web Application Keep Track of its Users?

If you have an Gmail account, you have probably observed that when you visit http://www.gmail.com, sometimes it asks for your username and password, but sometimes it takes you directly into your inbox; Similarly, when you visit http://www.amazon.com, you may be surprised that the front page has your first name on it! How do websites like Google and Amazon keep track of user identity and information? In this article, we are going to explore this topic, and explain how web applications use cookies to track their users over time.

Web applications do not track users by default

The core technologies that power the Internet are designed such that when you make requests to web servers, for example, Google's front page http://www.google.com, the web server does not know who you are, nor does it have any memory of its past interactions with you. The protocol that governs the transportation of data between your browser and Google's server, which is called