Skip to content

Instantly share code, notes, and snippets.

@Chryus
Chryus / gist:7477332
Created November 15, 2013 00:56
FizzBuzz with Ruby
#prints numbers 1-100
#when the number is divisible by 3, say fizz
#when the number is divisible by 5 say buzz
#when the number is divisible by 3 and 5 say fizzbuzz
i = 1
while i < 101
if i % 3 == 0
puts "fizz"
elsif i % 5 == 0
@Chryus
Chryus / gist:7478071
Created November 15, 2013 02:17
Ruby fizzbuzz with each + case statement
(1..100).each do |x|
m3 = x.modulo(3) == 0
m5 = x.modulo(5) == 0
puts case
when (m3 and m5) then 'FizzBuzz'
when m3 then 'Fizz'
when m5 then 'Buzz'
else x
end
@Chryus
Chryus / gist:7723531
Last active December 29, 2015 20:28
Example using attr_accessor to transform data (variables) in messages (methods).
class Artist
#attr_accessor is ruby magic that transforms instance variables into reader & writer methods (see lines 14-22)
attr_accessor :name, :songs
ARTISTS = []
def initialize
@name = name
@songs = []
#for each instance of the class artist we instantiate(self), we push it into the ARTISTS array via the shovel method.
@Chryus
Chryus / gist:7818687
Last active December 30, 2015 10:49
The example below uses regular expressions to parse through a string in timecode format hours:minutes:seconds,milliseconds. The format is used in SubRip (.srt) files to denote the interval of time that a subtitle should appear in a video.
#assign .srt file string to a variable
string = "00:03:10,500 --> 00:00:13,000"
#write regex to betak string into its constitute timecode parts and assign it to var reg_time
#captured data is anything between the parentheses
#'?<>' syntax assigns a key to each snapshot of data, e.g., '?<hour1>' assigns the key hour1
reg_time = /(?<hour1>.*):(?<minutes1>.*):(?<seconds1>.*)--> (?<hour2>.*):(?<minutes2>.*):(?<seconds2>.*)/
#call .match method on reg_time, passing it the string as parameter to retreive the data captures
m = reg_time.match(string)
class Compliment
attr_accessor :message, :image, :color
COMPLIMENTS = ["You're pretty", "You're super-fantastic!"]
IMAGES = ["https://24.media.tumblr.com/cc65a152ab08459fde8e834b30f795a7/tumblr_mxis6kDogx1qdhrnoo1_250.gif", "https://24.media.tumblr.com/b1229bb51705118a2e0be539db74df71/tumblr_mxao3oRohX1sbf1g1o1_500.gif", "http://www.mycathatesyou.com/wp-content/gallery/gifs/6.gif"]
COLORS = ["#8C727B", "#8CBEB2", "F2EBBF", "#F3B562", "#F76363"]
HISTORY = []
def initialize
class Compliment
attr_accessor :message, :image, :color
COMPLIMENTS = ["You're pretty", "You're super-fantastic!"]
IMAGES = ["image1", "image2", image3", "image4"]
COLORS = ["#8C727B", "#8CBEB2", "F2EBBF", "#F3B562", "#F76363"]
HISTORY = []
def initialize
module ComplimentSite
class App < Sinatra::Application
enable :sessions
get '/' do
if session[:last_color].nil? && session[:last_message].nil?
@compliment = Compliment.new
session[:last_color] = @compliment.color
session[:last_message] = @compliment.message
else
@compliment = Compliment.new
>>> 3+2
5
>>> typeof(3);
number
>>> typeof(3) === typeof(4.32);
true
>>> 5/0
@Chryus
Chryus / gist:8532688
Last active January 3, 2016 23:09
beginner functions in JavaScript
//this function finds the absolute value of a number
function absolute(num) {
if (num < 0) {
return -num;
} else {
return num;
}
}
@Chryus
Chryus / gist:8533713
Created January 21, 2014 02:55
absolute value function in JavaScript
function absolute(num) {
if (num < 0) {
return -num;
} else {
return num;
}
}
console.log(absolute(-10));