Skip to content

Instantly share code, notes, and snippets.

View spanuska's full-sized avatar

Skylar Paul spanuska

View GitHub Profile
example_tree = [
{:node=>:at_rule,
:name=>"charset",
:prelude=>[{:node=>:whitespace, :pos=>8, :raw=>" "}, {:node=>:string, :pos=>9, :raw=>"\"UTF-8\"", :value=>"UTF-8"}],
:tokens=>
[{:node=>:at_keyword, :pos=>0, :raw=>"@charset", :value=>"charset"},
{:node=>:whitespace, :pos=>8, :raw=>" "},
{:node=>:string, :pos=>9, :raw=>"\"UTF-8\"", :value=>"UTF-8"},
{:node=>:semicolon, :pos=>16, :raw=>";"}]},
{:node=>:style_rule,
{
"name": "dst-react",
"devDependencies": {
"browserify": "~>6.3",
"browserify-incremental": "^1.4.0",
"jasmine-expect-jsx": "^1.1.2",
"jest-cli": "^0.5.4",
"react": "^0.13.0",
"react-tools": "^0.12.1",
"reactify": "^0.17.1"
function nth(list, index) {
for (var node = list; node; node = node.rest) {
if (index == 0) {
return (node.value || undefined);
} else {
nth(node.rest, index--)
}
}
}
var i = 0;
console.log(i++);
var i = 0;
console.log(i += 1);
var i = 0;
console.log(++i);
@spanuska
spanuska / shelf.rb
Created March 31, 2016 13:28
Shelf with a dependency-managing wrapper method
class Shelf # without the wrapper
...
def store(book)
# imagine complicated logic here...
intermediate_result = book.storage_requirements / self.capacity
# imagine more complicated logic...
end
end
class Shelf # with wrapper
@spanuska
spanuska / book.rb
Last active March 30, 2016 13:27
Book with named keyword arguments
class Book
....
def initialize(title: 'Unknown', author: 'Unknown') # This is the Ruby >= 2.0 syntax.
@title = title
@author = author
end
end
class Shelf
...
def store(book)
length = book.length
width = book.width
depth = book.depth
# logic to find the nearest spot that can accomodate the given dimensions
end
end
@spanuska
spanuska / book.rb
Last active March 29, 2016 23:38
Book with default arguments hash
class Book
....
def initialize(args)
args = defaults.merge(args) # If you're using Ruby <2.0, this is the way to go.
@title = args[:title]
@author = args[:author]
end
def defaults
{title: "Unknown", author: "Unknown"}
end
class Shelf
...
def store(@book)
# logic to find the correct location on itself for @book
end
end
class Book
attr_accessor :title, :author
def initialize(args)
class Book
attr_accessor :title, :author
def initialize(args)
args = defaults.merge(args)
@title = args[:title]
...
end
def store_book
# logic for storing the book at a location on the shelf