Some explanation and examples of Ruby blocks. Encouraged to follow each file in the correct numbered order.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flist(list) | |
clone = list.dup | |
return [] if list == [] | |
[ clone.shift, clone ] | |
end | |
def len(list, acc = 0) | |
return acc if list == [] | |
_, tail = flist list | |
len(tail, acc + 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CityDocument < ActiveRecord::Base | |
include PgSearch | |
self.table_name = 'city_document_view' | |
self.primary_key = 'city_id' | |
pg_search_scope :_not_so_expensive_search_with_coords, (lambda do |term, lat, lon| | |
{ | |
query: term, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# exact point | |
3.0 ^ ((0.0 + 1.0) ^ -0.1) => 3.0 | |
# getting longer | |
3.0 ^ ((0.5 + 1.0) ^ -0.1) => 2.87 | |
3.0 ^ ((100 + 1.0) ^ -0.1) => 1.99 | |
# so far | |
3.0 ^ ((99999999 + 1.0) ^ -0.1) => 1.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# city_document_view | |
def up | |
execute <<-SQL | |
CREATE MATERIALIZED VIEW city_document_view AS | |
SELECT cities.id as city_id, | |
cities.name as city_name, | |
cities.population as population, | |
cities.longitude as longitude, | |
cities.latitude as latitude, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class City < ActiveRecord::Base | |
include PgSearch | |
belongs_to :region | |
belongs_to :country | |
has_many :city_aliases | |
## | |
# Search against a single field is not so expensive, | |
## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pg_search_scope :_expensive_search_with_coords, (lambda do |term, lat, lon| | |
{ | |
query: term, | |
against: { name: 'A' }, | |
associated_against: { | |
city_aliases: { name: 'B' }, | |
region: { name: 'C' }, | |
country: { name: 'D' } | |
}, | |
ignoring: :accents, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
City._cheap_search('london') # milliseconds | |
City._expensive_search('london') # more results, but more expensive (seconds) | |
City._expensive_search_with_coords('london', 34.54426, -91.96903) # smart, but way expensive (lotsa seconds) |
One can say that programming is not for everyone, but truth is we often miss the opportunity to find passionate people on teaching that think out-of-the-box and try to create a common bridge of communication so anyone really interested can smoothly grasp concepts such as:
- Computer CPU's speak bits
- Assembly is nice, saves us much time from typing bits for the processor, but it's not portable
- Programming languages are just specifications and abstractions which make our lives easier. But someone has to implement such specs in an already existing language
- Type systems made easy®
I hereby claim:
- I am leandronsp on github.
- I am leandronsp (https://keybase.io/leandronsp) on keybase.
- I have a public key ASBuO-wv2r1vTmLizbyfx3YiPldG8on4B3jknrMFX_JUpQo
To claim this, I am signing this object:
OlderNewer