Skip to content

Instantly share code, notes, and snippets.

@ryanveroniwooff
Created May 2, 2019 05:06
Show Gist options
  • Save ryanveroniwooff/222a8cbc3d2a130bcefc63d324b0be4e to your computer and use it in GitHub Desktop.
Save ryanveroniwooff/222a8cbc3d2a130bcefc63d324b0be4e to your computer and use it in GitHub Desktop.
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:
puts 5 # output => 5
# We can also output text:
puts "hello, world!" # output => hello, world!
# Back to variables for a second, we can create variables in ruby very easily:
username = "Ryan"
age = 20
puts username + " is " + age.to_s + " years old!" # output => Ryan is 20 years old!
# So in the username variable we have stored a string and in the age variable we have stored an integer
# Integers are the data types for numbers and strings are the data types for words.
# These are the two most basic data types
# Moving on:
string = "string"
character = 'c'
integer = 7
boolean = false
array = [string, character, integer.to_s, boolean]
puts array[1] # output => c
# Arrays are the most important data type to understanding code
# integers and strings are easy but this is where it gets fun
# Arrays can be used to store a data set
# In the above examples, all of our variables are stored to the same place
# This means we can acess all of the data at once by referencing the indexes for the array
# The index starts counting at 0 and goes up to the size of the array minus 1
# If your array has 4 values, the index range is 0 to 3
# In the above example we call array[1] and instead of getting the first value we get the second
# Soon you will forget that humans begin counting at 1, but for now it will probably goof you up good
# So we've got this nifty array setup with some nice data, but what good is it?
# A lot. We can use this baby to store so much data and then reference that data programmatically without sifting through it with our hands
# That brings us to Object-Oriented Programming or OOP
# OOP is a type of coding method where you define the types and functions of given data structures
# We won't dive into data structures yet, but arrays are the first step to understanding them
# Imagine a payroll system for instance:
# This would have a variable to store a users name, hours worked, pay rate, and also calculate their earnings
# We could think of it like this:
user = "Ryan"
hours = 10
rate = 10.75
total = hours * rate
# Then we could output that information like this:
puts user + " worked " + hours.to_s + " hours at \$" + rate.to_s + "\/hr" + " for a total of\: \$" + total.to_s + "\."
# This would output => Ryan worked 10 hours at $10.75/hr for a total of: $107.5.
# Which works great if we only have one person, but if we have a bunch, how can we handle that?
# We could type all of that out OR we could use OOP to do it for us
def get_payroll(data)
data.each do |e|
puts e[0] + ": worked " + e[1].to_s + "hrs \@ \$" + e[2].to_s + "\/hr" + " for a total of: $" + (e[1]*e[2]).to_s
end
end
data = [
["Ryan", 10, 10.75],
["Zack", 100, 0.75],
["Ben", 1, 10000],
["Sean", 40, 10],
["Nick", 60, 12],
["Jack", 160, 160],
]
get_payroll(data)
# This will ouput all of the information from the array in the desired format 'User: worked X hours at $X/hr for a total of $XXX.XX.'
# Now imagine we werent explicitly writing that data out and in fact the hours were tracked when employees clocked in and out
# This makes it so that we can send it the same type of data set with different information and get the same result
# Let's take a look at functions in Ruby
# First we define the function by giving it a name, parenthesis and an end:
def func()
end
# We can then call the function by typing its name:
func()
# Ok, lets make a new function now called output that accepts a variable called 'str'
def output(str)
# Then lets have it output the variable its given in the argument
puts str
end
# Now lets call it and send a string argument
output("Functions are cool\!")
# GREAT! You now have a basic understanding of:
# - variables and their data types
# - arrays and data structures
# - how to write functions
# That's exactly what you need to start your first project
# If you're ready, you can start now:
# Create a new file and write a program that will output all of the numbers from 1 to 100
# MY SOLUTION: ((50*2)+(100*9*9*0+600*0*5**2)).times{|i| puts i+1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment