Hack hack hack...

An open journal-- some of it written for you, but most of it is for me.

Lecture 6 Ruby- Class Methods

Strategy…

–>look for nouns to start url- unified resource locator map and collect –> returns the results of the operation–> so it uts it into a new array

select when you are trying to match something, cannot collect things conditionally (#) => library.songs.select{|s| s.name ==song_name}

is_a? –> is this an array, this is a validator

HOW TO BUILD CLASS METHODS methods that don’t apply to the instance of the class but the class itself you can tell if it’s a class method if self is the receiver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Artist
puts self

def self.top_5
end

end

def self.top_5
  self.all.sort_by{|a| a.rank}
end
end

#Another way...

class Artist
@@all = []
attr_accessor :name, :songs, :artists

def initialize
  @@all << self
  @songs = []
  @artists = []
end
#important pattern of observing intialization and making classes intelligent

def songs
  artists.collect{|artist| artist.song}
end

def self.all
  @@all #return all the artists ever created
end

3:30 PM MODULES - for creating all sort of shared behavior - modules don’t have instances–> the difference between modules and classes

two keywords to include behavior–> include and extend diff between include and extend –> trying to describe the scope of the module behavior

extending- makes it a class methods include –> methods in that module are instance methods ?? how does require fit into this??

3:38 ?? order of attr_accessor and @@all? does it matter?

INHERITANCE – a pattern that we don’t like as much as modules we’d rather think of things in the builder pattern using include and extend

2nd HALF

File Systems: Dir documentation (4:10) foreach and Dir.entries

ERB templates (4:23): we care about the result method for templating

PRIVATE AND PUBLIC METHODS (4:41) ->a private method cannot be called with an explicit receiever, only implicit receivers– means they can only be called in the class

GEMS: -a pre-built package or functionality See the Ruby Toolbox

SINATRA (4:44): -based on rack (the first ) RACK - create a class that responds to a method called ‘call’ - the response must return 3 three things- a status code, the header, the body of the response

1
ruby -rubygems sinatra.rb

SCRAPERS: nokogiri gem will xpath– path to an element or you can use an CSS adjustment nokogiri comes with a method called content rails cast on nokogiri: http://railscasts.com/episodes/190-screen-scraping-with-nokogiri/

httparty- some reading on httparty there is more reading here

open data: freebase

Comments