Skip to content

Instantly share code, notes, and snippets.

View Amokrane's full-sized avatar
🎯
Focusing

Amokrane Chentir Amokrane

🎯
Focusing
View GitHub Profile
@Amokrane
Amokrane / method_missing_super.rb
Created May 22, 2011 11:57
Using method_missing properly by calling `super`
# This snippet is taken from:
# "The Polite Programmer's Guide to Ruby Etiquette"
# http://bit.ly/hrwdPY
class BlackMagicStereo < Stereo
def method_missing(name, *args)
if sym.to_s =~ /^play_(\w+)$/
system("open songs/#{$1}.m4a")
else
# Here, very important!
@Amokrane
Amokrane / method_missing_respond_to.rb
Created May 22, 2011 12:27
respond_to? And method_missing
# This snippet is taken from:
# "The Polite Programmer's Guide to Ruby Etiquette"
# http://bit.ly/hrwdPY
class BlackMagicStereo < Stereo
def method_missing(name, *args)
if sym.to_s =~ /^play_(\w+)$/
system("open songs/#{$1}.m4a")
else
# Here, very important!
@Amokrane
Amokrane / method_missing_alias.rb
Created May 22, 2011 12:32
Making an alias of the method_missing
# This snippet is taken from:
# "The Polite Programmer's Guide to Ruby Etiquette"
# http://bit.ly/hrwdPY
class BlackMagicStereo < Stereo
# Make an alias of the previous method_missing definition
alias method_missing_without_show method_missing
def method_missing(name, *args, &block)
if sym.to_s =~ /^play_(\w+)$/
@Amokrane
Amokrane / method_missing_module.rb
Created May 22, 2011 12:37
Put your method_missing inside a module
# This snippet is taken from:
# "The Polite Programmer's Guide to Ruby Etiquette"
# http://bit.ly/hrwdPY
class BlackMagicStereo < Stereo
module GreyMagic
def method_missing(name, *args)
if sym.to_s =~ /^play_(\w+)$/
system("open songs/#{$1}.m4a")
else
@Amokrane
Amokrane / method_missing_super.rb
Created May 22, 2011 12:51
Using method_missing properly by calling `super`
# This snippet is taken from:
# "The Polite Programmer's Guide to Ruby Etiquette"
# http://bit.ly/hrwdPY
class BlackMagicStereo < Stereo
def method_missing(name, *args)
if sym.to_s =~ /^play_(\w+)$/
system("open songs/#{$1}.m4a")
else
# Here, very important!
@Amokrane
Amokrane / method_missing_respond_to.rb
Created May 22, 2011 12:52
Pair your method_missing with a respond_to?
# This snippet is taken from:
# "The Polite Programmer's Guide to Ruby Etiquette"
# http://bit.ly/hrwdPY
class BlackMagicStereo < Stereo
def method_missing(name, *args)
if sym.to_s =~ /^play_(\w+)$/
system("open songs/#{$1}.m4a")
else
# Here, very important!
@Amokrane
Amokrane / fibonacci_benchmark.rb
Created June 15, 2011 22:16
Example of using benchmark to measure the fibonnaci algorithm. Credit @tenderlove.
require 'benchmark'
def fib n
a = 0
b = 1
n.times { a, b = b, a + b}
a
end
Benchmark.bm(7) do |x|
@Amokrane
Amokrane / ex_fibonacci_benchmark.rb
Created June 15, 2011 22:21
Example of using benchmark to measure the fibonnaci algorithm. Credit @tenderlove.
require 'benchmark'
def fib n
a = 0
b = 1
n.times { a, b = b, a + b}
a
end
Benchmark.bm(7) do |x|
@Amokrane
Amokrane / benchmark_example2.rb
Created June 15, 2011 22:28
Benchmarking the fibonacci sequence through an increasing number of iterations. Credit @tenderlove.
require 'benchmark'
def fib n
a = 0
b = 1
n.times { a, b = b, a + b}
a
end
Benchmark.bm(10) do |x|
@Amokrane
Amokrane / attr_reader_vs_method.rb
Created June 15, 2011 22:52
Superficial improvements shown by @tenderlove at Rubyconf
def some_attribute
@some_attribute
end
# vs
attr_reader :some_attribute