Skip to content

Instantly share code, notes, and snippets.

@ctford
Created March 2, 2013 09:10
Show Gist options
  • Save ctford/5070246 to your computer and use it in GitHub Desktop.
Save ctford/5070246 to your computer and use it in GitHub Desktop.
Here's a quick run through of everything we've covered so far...
# First, you need to install ruby. This will be different for Windows, Linux and Mac.
# You're probably best googling instructions for your operating system.
#
# Once you've got it installed, there are two ways of running ruby code:
#
# 1) Type "irb" on your command-line, and start typing in expressions that
# will be immediately evaluated.
# 2) Save some ruby in a file ending with .rb, and then run it with the ruby
# command, e.g. "ruby my_code.rb". This will run everything in the file
# at once.
# Some simple ruby expressions. I'll use ?> to mean that we have irb open,
# and => to show the result of the expression:
#
# ?> 1 + 1
# => 2
#
# ?> "hello" * 3
# => "hellohellohello"
#
# ?> print("hello world!")
# hello world! => nil
#
# ?> a = 3
# => 3
#
# ?> a + 4
# => 7
# Ruby ignores any text after an "#", so if you load this file in irb or run it with
# the ruby command, this sentence will be ignored.
# We often want to define our own ruby commands, called "functions":
def greet(name)
print("Hello " + name)
end
# If you save function definitions in a .rb file, you can load them in irb like this:
# ?> load("my_code.rb")
# => true
#
# ?> greet("Jo")
# Hello Jo => nil
#
# Ruby functions start with the keyword "def" and end with the keyword "end". We then
# give the name of the function, and then arguments to that function between ()s.
#
# It's important to remember that literal pieces of text have double quotes around them,
# and ruby variables don't. So in the greet function, "Hello " is a piece of text
# consisting of six letters, but name is a variable that represents whatever text
# is passed into the function, in this case "Jo".
# In ruby, if statements are used to make choices.
def min(a, b)
if a < b
return a
else
return b
end
end
# Note that if we want a function to give an answer back, we use return.
# Remember to save and load your file each time you make a change.
# ?> load("my_code.rb")
# => true
#
# ?> min(11, 3)
# => 3
# If statements need their own end, just like function definitions. They can have an
# else, but they don't need to.
# You can use functions you define to build bigger and more complex functions.
# Below we've defined a function that will run the fizzbuzz game, printing "Fizz"
# when the number is a multiple of 3, "Buzz" when it's a multiple of 5, "Fizzbuzz"
# when it's a multiple of both, and just print out the number otherwise.
def is_fizz?(n)
if (n % 3 == 0)
return true
else
return false
end
end
def is_buzz?(n)
if (n % 5 == 0)
return true
else
return false
end
end
def fizzbuzz(size)
1.upto(size) do |n|
if is_fizz?(n) && is_buzz?(n)
puts("Fizzbuzz")
elsif is_fizz?(n)
puts("Fizz")
elsif is_buzz?(n)
puts("Buzz")
else
puts(n)
end
end
end
# Ruby lets you to build big things out of simple parts. For example, you can
# easily create lists of things in ruby using square brackets:
# ?> [1, 2, 4, 8, 16]
# => [1, 2, 4, 8, 16]
#
# ?> ["James", "Rebecca", "Christine"]
# => ["James", "Rebecca", "Christine"]
#
# You can get a specific element from a list using square brackets and an index:
# ?> people = ["James", "Rebecca", "Christine"]
# => ["James", "Rebecca", "Christine"]
#
# ?> people[0]
# => "James"
#
# ?> people[2]
# => "Christine"
#
# You can also go through a list (also known as an array) and do something for each element:
def greet_everyone(people)
people.each do |person|
puts("Hello " + person)
end
end
# ?> greet_everyone(["James", "Rebecca", "Christine"])
# Hello James
# Hello Rebecca
# Hello Christine
# => ["James", "Rebecca", "Christine"]
# We can also transform one list into another one:
def double_all(numbers)
numbers.map do |n|
n * 2
end
end
# ?> double_all([1, 4, 60])
# => [2, 8, 120]
# Another way to group together simple things into a bigger thing is with hashes:
#
# ?> john = {"first_name" => "John", "second_name" => "Smith", "age" => 77}
# => {"first_name" => "John", "second_name" => "Smith", "age" => 77}
#
# Here we've created a hash with information about John. We can write functions
# that take whole hashes as arguments:
def is_older_than?(person1, person2)
if person1["age"] > person2["age"]
return true
else
return false
end
end
# ?> john = {"first_name" => "John", "second_name" => "Smith", "age" => 77}
# => {"first_name" => "John", "second_name" => "Smith", "age" => 77}
#
# ?> susan = {"first_name" => "Susan", "second_name" => "Boyle", "age" => 55}
# => {"first_name" => "Susan", "second_name" => "Boyle", "age" => 55}
#
# ?> is_older_than?(john, susan)
# => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment