Skip to content

Instantly share code, notes, and snippets.

View ph3nx's full-sized avatar

ph3nx ph3nx

View GitHub Profile
@ph3nx
ph3nx / list_to_frame.rb
Created January 18, 2014 18:32
This Ruby program is a ruby function / method that prints a list / array in a rectangular frame of stars.
def list_to_frame list=[]
list = ["Hello", "World", "in", "a", "frame"] if list.empty?
longest = 0
list.each do |element|
longest = element.length if longest < element.length
end
(0..longest+3).each do
@ph3nx
ph3nx / english_2_pig_latin.rb
Created January 18, 2014 18:51
Ruby program with functions that translate english to Pig Latin and back to English. English is translated to Pig Latin by taking the first letter of every word, moving it to the end of the word and adding ‘ay’. “The quick brown fox” becomes “Hetay uickqay rownbay oxfay”.
def english_2_pig_latin string
string.split(/ /).each do |word|
word << word[0] << "ay "
print word[1..word.length]
end
end
@ph3nx
ph3nx / splat_arguments.rb
Created January 19, 2014 16:42
Example Ruby method with a splat argument. Splat arguments are preceded by a *. You use them when you don't know how many arguments there will be.
def greet message, *people
people.each { |p| puts "#{message}, #{p}!" }
end
greet "Hello", "Tony", "Ann", "BadMan"
@ph3nx
ph3nx / divisible_by_three?.rb
Created January 19, 2014 16:54
Ruby method that checks whether a number is evenly divisible by three or not. The function uses Ruby's ternary operator, which is a shorthand for the if-else control structure. It's a best pratice to end method names with a question mark when they return boolean values (true || false).
def by_three? number
number % 3 == 0 ? true : false
end
by_three? 5
# => false
by_three? 9
# => true
@ph3nx
ph3nx / combined_comparison_operator.rb
Created January 19, 2014 17:55
The combined comparison operator is used to compare two objects in Ruby. It returns 0 if they are the same. 1 if the first operand is greater and -1 if it's less.
a1 = "A Test"
a2 = "A Boom"
a1 <=> a2
# => 1
b1 = "2"
b2 = "9"
b1 <=> b2
@ph3nx
ph3nx / array_sorting.rb
Created January 19, 2014 20:45
Some ways to sort arrays in Ruby. Copy and paste the code examples in the irb console to try them.
array = [3,7,1,9,2,12,34,0]
# sort in ascending order
array.sort
# => [0, 1, 2, 3, 7, 9, 12, 34]
# sort in descending order
array.sort.reverse
# => [34, 12, 9, 7, 3, 2, 1, 0]
@ph3nx
ph3nx / alphabetize.rb
Last active June 28, 2018 02:26
The Ruby method "alphabetize" sorts an array in alphabetical order. If you wish put true as second argument to sort in descending order.
def alphabetize arr, rev=false
if rev
arr.sort.reverse
else
arr.sort
end
end
puts alphabetize ["b","e","a"], true
# => ["e","b","a"]
@ph3nx
ph3nx / benchmark_string_vs_symbol.rb
Created January 20, 2014 19:57
Simple Ruby benchmark to benchmark strings and symbols.
require 'benchmark'
string_AZ = Hash[("a".."z").to_a.zip((1..26).to_a)]
symbol_AZ = Hash[(:a..:z).to_a.zip((1..26).to_a)]
string_time = Benchmark.realtime do
100_000.times { string_AZ["r"] }
end
symbol_time = Benchmark.realtime do
@ph3nx
ph3nx / case.rb
Created January 20, 2014 21:14
Some examples for the case method in Ruby.
case var
when 1
puts 1
when 2
puts 2
else
puts 'none'
end
case var
@ph3nx
ph3nx / round_down_proc.rb
Created January 21, 2014 21:58
Ruby example that explains the usage of Proc's.
floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911]
# Write your code below this line!
round_down = Proc.new { |f| f.floor }
# Write your code above this line!
ints = floats.collect(&round_down)