Girl Develop It is here to provide affordable programs for adult women interested in learning web and software development in a judgment-free environment.
Girl Develop It is dedicated to providing a harrasment free learning experience for everyone.
For more information, see our Code of Conduct.
How was last week's homework?
Do you have any questions or concepts that you'd like to discuss?
chatting = true
byes = 0
while chatting
puts "Ask Grandma a question:"
said = gets.chomp!
if said == "BYE"
byes += 1
if byes > 2
chatting = false
else
puts "** Grandma is ignoring you **"
end
elsif said == said.upcase
year = 1930 + Random.rand(20)
puts "NO, NOT SINCE #{year}!"
else
puts "HUH?! SPEAK UP, SONNY!"
end
end
puts "Thanks for chatting with Grandma"
if
, elsif
, else
while
, for
[1,2,3]
[1..3]
{"one" => 1, "two" => 2}
A method is a name for a chunk of code
Methods are often used to define reusable behavior.
Let's look at a method we've already used:
1.even?
=> false
"fred".capitalize
=> "Fred"
"fred".class
=> String
If you have code you want to reuse or organize, methods will help.
Just like with loops and conditionals there is a term used to declare a method, def
and end
def subtract(x, y)
x - y
end
def
means define
subtract
is the name of the method
x, y
are parameters
x - y
is the body of the method
x - y
is also the return value
Arguments are passed
and parameters are declared.
Note that the variable names don't have to match!
In this code, 5
is an argument and x
is a parameter
def subtract(x,y) #parameters
x - y
end
subtract(5, 2) #arguments
Every Ruby method returns something.
Usually, it's the last statement in the method.
It could be the value sent to return if that came first.
def subtract(x,y)
x - y
'another thing'
end
return_value = subtract(5, 2)
puts return_value
def subtract(x,y)
return x - y
'another thing'
end
return_value = subtract(5, 2)
puts return_value
A local variable is a variable whose scope ends (goes out of context) when the function returns
def doubleThis num
numTimes2 = num*2
puts num.to_s+' doubled is '+numTimes2.to_s
end
doubleThis 44
puts numTimes2.to_s
44 doubled is 88
# >
Let's write a method takes in a String question
and returns an answer.
def method_name(parameter)
# method implementation
end
Only in 1.9 or newer
The Splat or asterisk operator will capture however many arguments you pass into greet
.
def greet(greeting, *names)
names.each do |name|
puts "#{greeting}, #{name}!"
end
end
>> greet("Hello", "Alice", "Bob", "Charlie")
Hello, Alice!
Hello, Bob!
Hello, Charlie!
For more explaination check out this link
If using 1.9 or lower, these have to be the last arguments in your list.
Default values are used if the caller doesn't pass them explicitly.
def eat(food = "chicken")
puts "Yum, #{food}!"
end
>> eat
Yum, chicken!
>> eat "arugula"
Yum, arugula!
def add_to_x_and_y(amount, vals)
x = vals[:x]
y = vals[:y]
x + y + amount
end
add_to_x_and_y(2, {:x => 1, :y => 2})
To pass variable parameters, or to pass named parameters,
you can use an options hash.
def bake(name, options = {})
flour = options[:flour] || "rye" #this is like setting a default value
creamer = options[:creamer] || "cream"
puts "baking a nice #{flour} loaf with #{creamer}"
end
bake("Wheat")
bake("Sourdough", :flour => "sour")
bake("Pumpernickel", :creamer => "butter")
Lets write a method called englishNumber
. It will take a number, like 22, and return the english version of it (in this case, the string 'twenty-two'). For now, let's have it only work on integers from 0 to 100.
An Object is an instance of a Class
Objects do things and have properties.
What can it do?
What are some properties?
Objects can be grouped by Class.
Classes define a template for objects. They describe what a certain type of object can do and what properties they have in common.
There are many kinds of objects, includingString
, Number
, Array
, Hash
, Time
, ...
To create an object we use .new
array = Array.new
array
now refers to an object instance
Methods can be defined on objects.
array = Object.new
def array.delete
puts "deleted the array"
end
array.methods(false)
Represent object state
Represent object state
Names start with an @
Only visible inside the object
cookie = Object.new
def cookie.add_chips(num_chips)
@chips = num_chips
end
def cookie.yummy?
@chips > 100
end
cookie.add_chips(500)
cookie.yummy? #=> true
Objects often take on names from their domain which makes them easy to conceptualize.
For example, without seeing my code, you can probably guess what an object called Bike
can do (or not do)
One thing that makes code easier to maintain is Encapsulation which is one of the fundamentals of OOP.
Encapsulation is a nice way of saying "put all my properties/behaviors in my capsule where other objects can't touch them".
By assigning certain responsibilities to objects, code is more purposeful and less likely to be changed by accident.
Just as you inherited traits from your mom, your objects may inherit traits from other objects.
Inheritance is when you use one class as the basis for another.
You might use Inheritance if two objects have the same properties and methods.
This way you don't need to write the same thing twice.
class MyString < String
end
my_string = MyString.new
my_string.upcase
Within OOP, this relates to different objects being able to respond to the same message in different ways.
[1, "abc"].each {|object| puts object.next}
Use the ask
method to enhance your text-based Adventure game.
How many other methods can you make for actions in your game? Be prepared to share!
Expand upon englishNumber. First, put in thousands. So it should return 'one thousand' instead of 'ten hundred' and 'ten thousand' instead of 'one hundred hundred'.
Expand upon englishNumber some more. Now put in millions, so you get 'one million' instead of 'one thousand thousand'.
Then try adding billions and trillions. How high can you go?
How about weddingNumber? It should work almost the same as englishNumber, except that it should insert the word "and" all over the place, returning things like 'nineteen hundred and seventy and two', or however wedding invitations are supposed to look. I'd give you more examples, but I don't fully understand it myself. You might need to contact a wedding coordinator to help you.
@gdidayton | #GDIDAY3
We are done with class 3!
We have done a lot, I know you have questions so ask them!