Skip to content

Instantly share code, notes, and snippets.

View ryanveroniwooff's full-sized avatar

Ryan Wooff ryanveroniwooff

View GitHub Profile
@ryanveroniwooff
ryanveroniwooff / simple.rb
Created May 2, 2019 05:06
Basic Concepts in Programming
# What is a data type?
# - Data types are the specified types of data stored by variables
# What are variables?
# - Variables are named objects created to store data in a location in the computers memory
# What do variables do?
# - Variables make it easier to work with data that we may not know yet, or may need to perform multiple operations
# - Generally it is best practice to name variables in a way that makes sense, and usually relates to the variables data type or the data itself
# Let's take a look at some examples
# In ruby, we output data by using the puts command:
@ryanveroniwooff
ryanveroniwooff / encrypt.rb
Last active May 3, 2019 23:03
A Simple Coding Project in Ruby
# Your challenge should you choose to accept it:
#****************
#* Preface **
#**************************************************************************************************
# Build a cipher to decode the message: "J\\wgdm\x1Ehq\x1D>mlhne-\x1EP`nsdm`dp\x1D,1sg!\x1E9'" **
# This message is passed as an argument(input) to the encrypt method along with a key **
# pp is better for testing than puts xD ** **
# program inherently outputs the return of the encrypt funcion by calling it in a pp statement ;)**
#**************************************************************************************************
#****************************
@ryanveroniwooff
ryanveroniwooff / simple_cipher.rb
Last active February 17, 2018 09:32
simple string encoding from key
def encode(message, key)
c = key.to_s.split('').map { |e| e.to_i }.cycle #setup key cycle
letter_hash = Hash[('a'..'z').zip(1..26)] #hash containing 'a' => 1, 'b' => 2, and so on...
message.split('').map { |e| letter_hash.fetch(e,e) + c.next} #encode vlaues
end
def decode(code, key)
c = key.to_s.split('').map { |e| e.to_i }.cycle #setup key cycle
h = Hash[(1..26).zip('a'..'z')] #hash containing 1 => 'a', 2=> 'b', and so on...
code.map { |e| h.fetch((e - c.next),e) }.join('') # decode and fetch values from hash and join
@ryanveroniwooff
ryanveroniwooff / pyramid.rb
Last active October 8, 2017 10:43
Creates a perfect pyramid of *'s in terminal. make a call to the method with your specified height
def make_stars(height)
star = '*'
space = ' '
arr = []
# Sets up non-formatted stars for given height
for i in 1..height
arr << [i == 1 ? star : star * ((i * 2) - 2)]
end
@ryanveroniwooff
ryanveroniwooff / christmas_tree.rb
Last active October 8, 2017 10:56
Download this file and run in your console for a merry christmas message!
# This Program creates and outputs a formatted and colorized chirstmas tree with "Merry Christmas!" below it
# This code handles colorization of console output
class String
# colorization
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
# readFile sets up a new file right away by calling writeFile
# then stores the file as readfile and returns the formatFile method on the readfile
def readFile
writeFile
file = File.open(File.expand_path File.dirname(__FILE__) + "/output.txt")
readfile = file.read
file.close
readfile.formatFile
end
@ryanveroniwooff
ryanveroniwooff / string_to_dollars.rb
Created June 14, 2017 00:19
Format ruby variable to us dollar
# use the .to_dollars method on any string and get appropriate
# dollar amount returned with two decimal places rounded to the hundreths
# format is for us dollar
class String
def to_dollars
d = self.decimals_present
return "$" + self + "00" if d == 0
return "$" + self + "0" if d == 1
return "$" + self if d == 2
return "$" + self.decimals[1] + '.' + self.round_decimals if d > 2
@ryanveroniwooff
ryanveroniwooff / sort.rb
Last active April 21, 2017 10:34
Array Sorting Method in Ruby (one liner included for funsies)
def sort(a, a_is_unsorted = true)
# (a_is_unsorted = false; (a.size - 1).times{ |i| j = i + 1; a[i], a[j], a_is_unsorted = a[j], a[i], true if a[i] > a[j]}) while a_is_unsorted
while a_is_unsorted do
a_is_unsorted = false
(a.length - 1).times{ |i|
a[i], a[i + 1], a_is_unsorted = a[i + 1], a[i], true if a[i] > a[i + 1]
}
end
a
end
@ryanveroniwooff
ryanveroniwooff / output.rb
Last active February 1, 2017 10:12
Outputs variable to file in ruby language
def self.output_to_file(item)
fname = "program-output.txt"
time = Time.new.strftime("%a, %b %d @ %I:%M:%S %p")
file = File.open(fname, "a") # a = append, w = wipe
file.puts "#{time}: #{item} \n\n"
file.close
end
@ryanveroniwooff
ryanveroniwooff / word-counter.rb
Last active February 1, 2017 00:07
Word Counting Algorithm - Utilizing the Hash
def count_words(s)
return nil if s.nil? || s.split.empty?
s.downcase.gsub(/[^a-z0-9'!\s-]/i, '').split(" ").each_with_object(Hash.new(0)){ |word,counts| counts[word] += 1 }
end