Skip to content

Instantly share code, notes, and snippets.

View ph3nx's full-sized avatar

ph3nx ph3nx

View GitHub Profile
@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 / timestamp.rb
Created January 16, 2014 08:36
Handy method to get the current unix time stamp in Ruby. Unix time, or POSIX time, is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.
def timestamp
Time.now.to_i
end
@ph3nx
ph3nx / prime_numbers.rb
Last active July 17, 2021 07:16
Ruby program / method that prints out all prime numbers until a given maximum.
def prime_numbers max
for i in (2..max) do
for j in (2..i) do
break if i%j == 0
end
p "#{i} is a prime number." if i == j
end
end
require 'prime'
@ph3nx
ph3nx / multiplication_table.rb
Created January 18, 2014 14:20
Ruby program that prints a multiplication table for numbers up to your input. For example 12 x 12 fields.
def tbl max
for y in (1..max) do
for x in (1..max) do
sol = y*x
if sol < 10
print "#{sol} "
elsif sol < 100
print "#{sol} "
else
print "#{sol} "
@ph3nx
ph3nx / jwplayer.html
Created February 26, 2014 12:13
This is how you setup the jwplayer with a self-hosted video file. The official documentation: http://www.longtailvideo.com/support/jw-player/28833/quick-start-guide
<!-- JW Player Library -->
<script src="http://jwpsrv.com/library/MCK8hplLEeOY0CIACmOLpg.js"></script>
<!-- div for the player -->
<div id='playerKXSDPIwKERSv'></div>
<!-- Script to display video file in the player div -->
<script type='text/javascript'>
jwplayer('playerKXSDPIwKERSv').setup({
// URL to the video file
@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 / random_string.php
Last active July 27, 2016 11:26
This code snipped shows you how to generate random strings in PHP.
<?php
function random_string($length) {
$LETTERS = 'abcdefghijklmnopqrstuvwxyz';
$random_string = '';
for ($i=0; $i < $length; $i++) {
$random_string = $random_string.$LETTERS[rand(0, strlen($LETTERS)-1)];
}
@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)
@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 / 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