Skip to content

Instantly share code, notes, and snippets.

@ryanckulp
ryanckulp / if_guard.rb
Created January 2, 2018 03:55
example if/guard clause in Ruby
# verbose
if 2 + 2 == 4
puts "Correct!"
end
# short-hand
puts "Correct" if 2 + 2 == 4
# more realistic short-hand
def addition_problem(user_answer, correct_answer)
@ryanckulp
ryanckulp / flash_cards.rb
Last active January 1, 2018 23:41
a sample flash card application in Ruby
questions = {
'one' => {text: "What is 2 + 2?", answer: 4},
'two' => {text: "What is 4 + 7?", answer: 11},
'three' => {text: "What is 7 + 3?", answer: 10},
}
user_answers = {
'one' => 4,
'two' => 9,
'three' => 10
@ryanckulp
ryanckulp / grocery_array.rb
Last active January 2, 2018 01:33
looping through an array of groceries in Ruby
groceries = ["apple", "pear", "banana"]
groceries.each do |grocery|
puts grocery
end
# =>
# apple
# pear
# banana
@ryanckulp
ryanckulp / student_age_array.rb
Created January 1, 2018 23:14
array of floats for looping in Array
ages = [16, 15.6, 16.25, 17.1, 16.4, 15.8]
total_years = 0.0
ages.each do |age|
total_years += age
end
total_years / ages.size # => 16.191666666666666
@ryanckulp
ryanckulp / eccentric_array.rb
Created January 1, 2018 23:07
array with different element types in Ruby
arr = [3, "four", {five: 'six'}]
@ryanckulp
ryanckulp / hash_actions.rb
Created January 1, 2018 22:54
adding key/values to a Hash in Ruby
questions = {} # => create an empty Hash/object
questions['one'] = "What's your name?"
questions['two'] = "How old are you?"
@ryanckulp
ryanckulp / hash_bracket.rb
Created January 1, 2018 22:53
accessing a Hash key/value in Ruby
questions = {
'one' => "What's your name?",
'two' => "How old are you?"
}
questions['one'] # => returns "What's your name?"
questions['two'] # => returns "How old are you?"
@ryanckulp
ryanckulp / hash.rb
Created January 1, 2018 22:51
hash example in Ruby
questions = {
'one' => "What's your name?",
'two' => "How old are you?"
}
@ryanckulp
ryanckulp / if_else_php.php
Created January 1, 2018 22:44
if/else example in PHP
<?php
$x = 4;
if ($x == 4) {
echo "Correct!";
} else {
echo "Inorrect!";
}
@ryanckulp
ryanckulp / if_else_java.java
Created January 1, 2018 22:40
if/else example in Java
int x = 4;
if (x == 4) {
System.out.println("Correct!");
} else {
System.out.println("Inorrect!");
}