Skip to content

Instantly share code, notes, and snippets.

View Rojo's full-sized avatar
🏠
Working from home

Rojo

🏠
Working from home
  • México
View GitHub Profile
@Rojo
Rojo / welcome_helper.rb
Created December 5, 2014 19:41
Trying to test a helper...
module WelcomeHelper
def social_share_button(network)
content_tag :div, class: "btn btn-#{network}" do
link_to ENV["#{network}_share"] do
image_tag "#{network}.png", class: 'social-image'
end
end
end
@Rojo
Rojo / book_index_sort.rb
Created September 29, 2014 15:54
Book Index Sort
# Sorts an array of decimals as if the were the chapters and sections of a book
sections = [3.1, 3.11, 3.2, 3.3, 3.8, 3.7 , 3.8, 3.9, 3.10]
sections.sort do |e1, e2|
if e1.to_i == e2.to_i # Comparing sections within the same chapter
# Get the real value for the section within the chapter
n1 = e1 + e1.to_s.split('.')[1].to_i
n2 = e2 + e2.to_s.split('.')[1].to_i
# Compare using the real value of the section
n1 <=> n2
@Rojo
Rojo / gist:b8340ab717ccee65a28f
Created August 19, 2014 12:56
.zlogin && .zshrc for RVM
# /home/$USER/.zlogin
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
# /home/$USER/.zshrc
ZSH_THEME=avit
plugins=(rails3 gitfast git-extras ruby bower bundler command-not-found common-aliases npm rvm sublime)
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
@Rojo
Rojo / int2pattern.rb
Created August 6, 2014 11:48
Int to Mixed Base (And Back)
# Couple of functions that transform a given integer into a mixed-base string
# representation and back. The string representation is formed by three 'A-Z'
# characters (base 26), followed by four '0-9' digits (base 10).
# Some examples:
# | Integer | Pattern |
# | 0 | AAA0000 |
# | 1 | AAA0001 |
# | 9999 | AAA9999 |
# | 10000 | AAB0000 |
@Rojo
Rojo / onion_layers.rb
Last active August 29, 2015 14:01
A simple recursion example.
def peel(vegetable_with_layers)
if vegetable_with_layers[0] == 'core'
# base case: have I reached the core?
vegetable_with_layers[0]
else
# not the core... remove one layer
peel(vegetable_with_layers[0])
end
end
@Rojo
Rojo / time_conventions.rb
Created May 21, 2014 14:17
Using method_missing to extend Fixnum to handle some time conventions in a more natural way.
# This combines the examples given here http://pastebin.com/zxsur5MX
# and here http://pastebin.com/agjb5qBF
# Note: Time.now returns current time as seconds since epoch
module TimeConventions
TIME_CONVENTIONS = {
'second' => 1,
'minute' => 60,
'hour' => 60 * 60,
}
@Rojo
Rojo / number_magnitude.rb
Created March 13, 2014 06:24
Label the magnitudes of a number.
def labelize number
magnitudes = [
[1e12.to_i, 'trillion'], [1e9.to_i, 'billion'], [1e6.to_i, 'million'],
[1e3.to_i, 'thousand'], [1e2.to_i, 'hundred']
]
left = number
magnitudes.each do | label |
if left / label[0] > 0
write = left / label[0]