Skip to content

Instantly share code, notes, and snippets.

@JoelQ
JoelQ / dir.rb
Last active August 29, 2015 14:13
__dir__ and others
# all outputs should be the same
puts __dir__
puts File.expand_path(__dir__)
puts File.expand_path File.dirname(__FILE__)
puts File.expand_path("../", __FILE__)
@JoelQ
JoelQ / dir_comparison.rb
Created January 8, 2015 16:45
__dir__ comparison
puts "__dir__ is the same as File.expand_path(__dir__)"
puts __dir__ == File.expand_path(__dir__)
puts "__dir__ is the same as File.expand_path( File.dirname(__FILE__) )"
puts __dir__ == File.expand_path( File.dirname(__FILE__) )
puts "__dir__ is the same as File.expand_path('../', __FILE__)"
puts __dir__ == File.expand_path("../", __FILE__)
# Script Output:
@JoelQ
JoelQ / .bash_profile
Created June 6, 2012 17:41
My bash_profile
# Show branch in status line
PS1='[\w]$ '
#PS1="\u$ \w: "
alias ll="ls -lahG"
alias subl='open -a "Sublime Text 2"'
# Git bash completion
if [ -f `brew --prefix`/etc/bash_completion ]; then
. `brew --prefix`/etc/bash_completion
@JoelQ
JoelQ / before.rb
Last active December 11, 2015 03:58
Example refactoring using Introduce Explaining Variable
def convert_to_media(link)
if URI.split(link)[5].end_with? 'png', 'gif', 'jpeg'
"<img src=\"#{link}\" />"
elsif URI.split(link)[2].include?("youtube") && URI.split(link)[7] && URI.split(link)[7].include?('v=')
video_id = URI.split(link)[7][2..-1]
"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/#{video_id}\" frameborder=\"0\" allowfullscreen></iframe>"
elsif URI.split(link)[2].include? "youtu.be"
video_id = URI.split(link)[5].gsub('/', '')
"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/#{video_id}\" frameborder=\"0\" allowfullscreen></iframe>"
else
@JoelQ
JoelQ / before.rb
Created January 15, 2013 19:45
Example refactoring for RenameMethod
require 'uri'
class MediaParser
def initialize(text)
@text = text
@links = URI.extract @text
end
def parse_links
@JoelQ
JoelQ / before.rb
Last active December 11, 2015 03:58
Example refactoring for Extract Method
require 'uri'
class MediaParser
def initialize(text)
@text = text
@links = URI.extract @text
end
def parse_links
@JoelQ
JoelQ / before.rb
Last active December 11, 2015 03:59
Refactoring example for Extract Class
require 'uri'
class MediaParser
def initialize(text)
@text = text
@links = URI.extract @text
end
def parse_links
@JoelQ
JoelQ / before.rb
Last active December 11, 2015 04:08
Refactoring example for Replace Conditional with Polymorphism
require 'uri'
class MediaParser
def initialize(text)
@text = text
@links = URI.extract @text
end
def parse_links

Ruby docs for Array#uniq

From the docs:

uniq → new_ary uniq { |item| ... } → new_ary Returns a new array by removing duplicate values in self.

If a block is given, it will use the return value of the block for comparison. It compares values using their hash and eql? methods for efficiency.

@JoelQ
JoelQ / intro_to_elm_slide_notes.md
Created June 15, 2016 19:55
Raw speaker notes for Intro to Elm talk. Slides at http://www.slideshare.net/thoughtbot/intro-to-elm

What is Elm

Pure Functions

What does that mean?

  • For a given input, you always get the same output
  • The output cannot depend on any external factors (e.g. File on disk, some external variable)