Skip to content

Instantly share code, notes, and snippets.

-- ex.1
SELECT isbn
FROM editions
INNER JOIN publishers ON editions.publisher_id = publishers.id
WHERE publishers.name = 'Random House';
-- ex.2
SELECT editions.isbn, books.title
FROM editions
INNER JOIN publishers ON editions.publisher_id = publishers.id
class Customer
attr_reader :name, :budget
def initialize(name, budget)
@name = name
@budget = budget
end
def within_budget?(price)
price < budget
end
class Comment
attr_reader :user, :content, :post_id
def initialize(user, content,post_id)
@user = user
@content = content
@post_id = post_id
end
end
# ex.1
require 'rubygems'
require 'rest-client'
wiki_url = "http://en.wikipedia.org/"
wiki_local_filename = "wiki-page.html"
File.open(wiki_local_filename, "w") do |file|
file.write(RestClient.get(wiki_url))
end
class Person
def self.example_class_method
puts "We're calling an example class method"
puts "'self' is always defined. What is 'self' here? Let's see."
p self
puts "That was self!"
end
def example_instance_method
puts "We're calling an example *instance* method"
module Flight
def Flight.fly(animal)
if animal.superclass.name == "Bird" || name = animal.name == "Bat"
"I'm a #{animal.name}, I'm flying!"
end
end
end
@pengjunp
pengjunp / ex1.rb
Created May 2, 2016 20:11
I/O exercises
require 'rubygems'
require 'rest-client'
wiki_url = "http://en.wikipedia.org/"
wiki_local_filename = "wiki-page.html"
File.open(wiki_local_filename, "w") do |file|
file.write(RestClient.get(wiki_url))
end
def count_letters (s)
result = Hash.new(0)
s.gsub(/\s/, "").each_char {|c| result[c] += 1}
puts result.inspect
end
count_letters("This is a random string that doesn't make any sense at all")
def letter_indices (s)
result = {}
def sign_price
# declare the total price
total = 0
# calculate the size
puts "Sign dimensions? length, width (feet)"
dimensions = gets.chomp.split(',', 2)
size = dimensions[0].to_f * dimensions[1].to_f
total += 15 * size
# calculate the colors
puts "How many colors?"

Pry Cheat Sheet

Command Line

  • pry -r ./config/app_init_file.rb - load your app into a pry session (look at the file loaded by config.ru)
  • pry -r ./config/environment.rb - load your rails into a pry session

Debugger