Skip to content

Instantly share code, notes, and snippets.

@rubyginner
rubyginner / manageDownload.rb
Created November 25, 2012 14:49
Organize your downloads
require 'fileutils'
path = '/Users/rubyginner/Downloads'
docs = ['.pdf', '.epub', '.txt', '.rtf']
pics = ['.png', '.gif', '.jpg', '.jpeg','.bmp', '.JPEG', '.JPG']
music = ['.mp3', '.wav']
videos = ['.mp4', '.wmv', '.flv', '.mov', '.mpeg']
apps = ['.dmg', '.pkg']
archive = ['.zip', '.tar']
@rubyginner
rubyginner / palindrome.rb
Created October 12, 2012 13:47
Check if a word is palindrome or not
def palindrome?(word)
return (word == word.reverse) ? true : false
end
puts "Enter a word to see if it's a palindrome..."
word = gets.chomp
puts "The word #{word} is #{palindrome?(word.to_s) ? 'a' : 'not a'} palindrome."
@rubyginner
rubyginner / list-photos.rb
Created October 11, 2012 08:01
Browse through sub-folders of photos
path = 'C:/folio-gallery/albums'
valid_files = ['.png', '.jpg', '.jpeg']
Dir.foreach(path) { |folder|
next if ['.', '..'].include? folder
puts "#{folder}"
Dir.foreach("#{path}/#{folder}") { |subfolder|
puts "- #{subfolder}" if valid_files.include? File.extname(subfolder)
}
}
@rubyginner
rubyginner / calculate-leaves.rb
Created October 11, 2012 06:54
Calculate remaining VL and SL
#calculate remaining VL and SL
def remaining_leaves(vl_taken, sl_taken)
month_now = Time.now.month
vl_remaining = ((month_now.to_f*1.25) - vl_taken.to_i)
sl_remaining = ((month_now.to_f*1.25) - sl_taken.to_i)
return "Remaining VL: #{vl_remaining.to_f} Remaining SL: #{sl_remaining.to_f}"
end
puts "How many VLs have you taken? "
@rubyginner
rubyginner / multiply-table.rb
Created October 11, 2012 05:27
Generate a multiplication table
# version 1
(1..10).each { |y|
(1..10).each { |x|
print "#{x * y} "
}
puts ''
}
# version 2
@rubyginner
rubyginner / foobar.rb
Created October 11, 2012 05:21
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Foo” instead of the number and for the multiples of five print “Bar”. For numbers which are multiples of both three and five print “FooBar”.
# version 1
for counter in 1..100
if (counter % 3 == 0) && (counter % 5 == 0)
puts 'FooBar'
elsif (counter % 3 == 0)
puts 'Foo'
elsif (counter % 5 == 0)
puts 'Bar'
else