Last active
December 30, 2015 22:39
-
-
Save randalmaile/7895303 to your computer and use it in GitHub Desktop.
Advanced Classes checkpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Parent classes should be as generic as possible. | |
- Super calls the parent's initilize method!! (hope this is true!!).. and the child (calling super) can pass in whatever parameters make sense within the context of that child initialize method!!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```Bloc output - Inheritance | |
``` | |
Shape can_fit? should tell if a shape can fit inside another shape | |
Shape color should be able to get and set color | |
Rectangle initialize should take width, height | |
Rectangle initialize should be able to set a color if given | |
Rectangle initialize should be able to set the default color to Red | |
Rectangle area should return correct area for large rectangle | |
Rectangle area should return correct area for a small rectangle | |
Square initialize should only take a side and color | |
Square initialize should set a default color | |
Square area should return right area | |
Circle initialize should only take radius and color | |
Circle initialize should set a default color | |
Circle initialize should not respond to width or height | |
Circle area returns the area of a small circle | |
Circle area returns the area of a large circle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. I'm a little confused on how to use the !(bang) in Ruby. For instance in this class, how is #{other.name}! working?: | |
class Animal | |
attr_accessor :name | |
def initialize(name) | |
@name = name | |
end | |
def eat(other) | |
puts "#{@name} ate #{other.name}! #{self.noise}" | |
end | |
end | |
2.In the Shape class example: | |
class Shape | |
attr_accessor :color | |
def initialize(color = nil) | |
@color = color || "Red" | |
end | |
def can_fit?(shapeInstance) | |
if self.area >= shapeInstance.area | |
true | |
else | |
false | |
end | |
end | |
end | |
...why are we setting color = nil as the initialize argument? | |
3. What would really really really help me out would be to have an editor that caught typos - I just killed 2 hrs. because I misspelled something :( ...help :) for instance - I spelled "initialize" - "initilize" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Inheritance | |
class Shape | |
attr_accessor :color | |
def initialize(color = nil) | |
@color = color || "Red" | |
end | |
def can_fit?(shapeInstance) | |
if self.area >= shapeInstance.area | |
true | |
else | |
false | |
end | |
end | |
end | |
class Rectangle < Shape | |
attr_accessor :width, :height | |
def initialize(width, height, color = nil) | |
@width, @height = width, height | |
super(color) | |
end | |
def area | |
p @width * @height | |
end | |
end | |
class Square < Rectangle | |
def initialize(side, color = nil) | |
super(side, side, color) | |
end | |
end | |
class Circle < Shape | |
attr_accessor :radius | |
def initialize(radius, color = nil) | |
@radius = radius | |
super(color) | |
end | |
def area | |
p Math::PI * (@radius * @radius) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment