Skip to content

Instantly share code, notes, and snippets.

# -*- coding: utf-8 -*-
# Quick rendition of Conway's game of life
class Grid
attr_reader :width, :height
CHARS=[
' ·•◌◉◍◎●',
' ·•◌◔◑◕◍●',
' ⚀⚁⚂⚃⚄⚅',
' ⚋⚊⚏⚍⚎⚌☷☳☵☶☱☲☴☰',
' ✡✢✣✤✥✦✧✨✩✪✫✬✭✮✯✰✱✲✳✴✵✶✷✸✹✺✻✼✽✾✿❀❁❂❃❄❅❆❇❈❉❊❋',
@wavded
wavded / promise.js
Last active May 6, 2021 13:25
Promise A+ Implementation
"use strict"
var Promise = function () {
this.state = 'pending'
this.thenables = []
}
Promise.prototype.resolve = function (value) {
if (this.state != 'pending') return
this.state = 'fulfilled'

Web Linguistics : Towards Higher Fluency

About the talk

The idea for this talk came with digging in to web security after last winter's Rails vulnerabilities. Whole categories of vulnerabilities are due to not properly escaping inputs, at least that is the common wisdom : ESCAPE ALL THE THINGS.

Fact is that having to do escaping manually will always be error prone. Using SafeBuffer to automatically escape in views/templates might seem like a good idea, but it's not addressing the structural problem : that we're manipulating structured data (like a DOM) as plain text.

It can be quite an epiphany to look at it from that angle. Plain text representations should be considered a serialization format. It is not sensible to write that by hand. It's more complex than is commonly acknowledged. What if you have a URL inside CSS inside JSON inside HTML?

require 'nokogiri'
require 'open-uri'
paragraphs = Nokogiri( open('http://en.wikipedia.org/wiki/Ruby_(programming_language)') )/'p'
# Print fromt the first paragraphs containing 'Google Tech Talk' to the first following paragraph containing 'mixins'
paragraphs.map(&:text).each do |p|
puts p if (p =~ /Google tech talk/i)..(p =~ /mixins/i)
end
@havenwood
havenwood / example.txt
Last active December 15, 2015 00:39
Small subset of `tail` command using Ruby 2.0.0 Enumerable::Lazy (inspired by RubyTapas 072)
line one
line two
line three
array = (1..12).map &:to_s
class Array
def insert_every n, this
each_slice(n).inject { |r, a| r << this << a }.flatten
end
end
array.insert_every 3, ','
#=> ["1", "2", "3", ",", "4", "5", "6", ",", "7", "8", "9", ",", "10", "11", "12"]
@havenwood
havenwood / enum_to_h.rb
Last active December 14, 2015 12:48
Enumerable#to_h
module Enumerable
def to_h
Hash[*self]
end
end
1.upto(4).to_h
#=> {1=>2, 3=>4}
[:first, 1, :second, 2].to_h
@vinirodr
vinirodr / argumentos_nomeados.rb
Created March 1, 2013 18:11
some ruby 2.0 features
# ruby 1.9 - Argumentos Ordenados
def params_1_9(name, age)
name = args[:name]
age = args[:age]
puts name
puts age
end
# ruby 2.0 - Argumentos Nomeados
@havenwood
havenwood / fibonacci.rb
Created February 28, 2013 23:17
Fibonacci Sequence with Ruby 2.0.0 Experimental Refinements
module Fibonacci
refine Fixnum do
def fibs
a, b = 0, 1
1.upto(self).with_object([]) do |_, array|
array << a
a, b = b, a + b
end
end
end
@havenwood
havenwood / additional_hash_methods.rb
Last active December 13, 2015 19:39
Hash#map_value, Hash#map_key, Hash#map_pair.
class Hash
def map_value
each_pair.with_object({}) do |(key, value), result|
result[key] = yield value
end
end
def map_key
each_pair.with_object({}) do |(key, value), result|
result[yield key] = value