I'm still working through Learn Ruby the Hard Way. The exercise I'm working on now has me building a text-adventure dungeon (similar to the one I made when I was going through Beginning Ruby) but this time around I've got a better idea of how to map the project out.
Basically I'm starting with a simple 4 room dungeon. Players will need to go to one room to get a key to unlock a door in the starting room. I understand how to build rooms and allow a player to navigate between them, but managing an inventory and allowing players to access different rooms once they have completed a specific action (such as finding a key to unlock the door) was confusing me.
After asking for some input on Stack Overflow, I think I've got a better handle on the problem now. I'd originally set door_open = false in the start_room method, then once a player had found the key in the chest_room method, I set door_open to true. However, I couldn't call this from the start_room, because it was a local variable. As far as start_room was concerned, door_open was still = false. Am I thinking about this the right way?
I'll simply make key into an instance variable, so it can be accessed by all methods, and edit the logic so while !@key_present, players can open the door. Easy enough! Just wish I could get to coding NOW instead of tonight.
Also, I've been using if/elsif statements, but the same user suggested case/when is cleaner and simpler. I haven't used case/when before, but from the snippet of code he posted, I think I agree. Your thoughts?
My name is Tyler. I'm learning Ruby on Rails. This is a blog for me to talk about what I've learned. I'm no longer completely new to Ruby and while I don't feel comfortable calling myself a coder or programmer yet, I am absolutely a Ruby Enthusiast and a Rails Fancier. I've been working with Ruby on Rails since February 1, 2011. You know that song about "lovin' every minute of it?" Bingo.
Showing posts with label logic. Show all posts
Showing posts with label logic. Show all posts
Monday, October 10, 2011
Monday, February 7, 2011
Subclasses and Superclasses in Ruby
So last night I began learning about Classes and inheritance. A class can only inherit from one other class. Makes sense, but the thing I was wondering about all day is this:
Say you have a parent class with a child class, that child class (child1) has a child class of its own.(child2) I am sure this kind of thing is a regular occurrence. So does child2 also inherit from the parent class in addition to inheriting from child1? My guess was yes, it does. So I came home and whipped up some code to test this theory.
class Pet
def method1
puts "I am a good pet."
end
end
class Cat < Pet
def method2
puts "I am a cat. Meow!"
end
end
class Oskar < Cat
def method3
puts "I am the BEST cat of all time."
end
end
oskar = Oskar.new
puts "Oskar says:"
oskar.method1
oskar.method2
oskar.method3
Success! Oskar is a child of the Cat class, which is a child of the Pet class, and Oskar inherits all the logic from the previous generations. Just as I suspected. So a class can only inherit directly from one class, as in Oskar < Cat, but it inherits directly from all classes that come before its parent in the great circle of life.
Is there a bit of code to list all the inherited logic for a particular class? Also, what do you call more than one child class? Childs? Children? Chirren?
Say you have a parent class with a child class, that child class (child1) has a child class of its own.(child2) I am sure this kind of thing is a regular occurrence. So does child2 also inherit from the parent class in addition to inheriting from child1? My guess was yes, it does. So I came home and whipped up some code to test this theory.
class Pet
def method1
puts "I am a good pet."
end
end
class Cat < Pet
def method2
puts "I am a cat. Meow!"
end
end
class Oskar < Cat
def method3
puts "I am the BEST cat of all time."
end
end
oskar = Oskar.new
puts "Oskar says:"
oskar.method1
oskar.method2
oskar.method3
Success! Oskar is a child of the Cat class, which is a child of the Pet class, and Oskar inherits all the logic from the previous generations. Just as I suspected. So a class can only inherit directly from one class, as in Oskar < Cat, but it inherits directly from all classes that come before its parent in the great circle of life.
Is there a bit of code to list all the inherited logic for a particular class? Also, what do you call more than one child class? Childs? Children? Chirren?
Saturday, February 5, 2011
Separating Logic and Output in Ruby
Just finished up Chapter 4 of Beginning Ruby, which walks you through creating a basic application that pulls information from a separate text file and performs a few calculations based on the contents of the file. This was a good exercise, because up until now I'd just been coding information directly into the file itself, and coding so that Ruby can pull information from elsewhere is obviously more versatile and useful.
But here's a question. It seems like it makes sense to separate logic from output. So that this bit of code:
word_count = text.split.length
puts "#{word_count} words"
paragraph_count = text.split(/\n\n/).length
puts "#{paragraph_count} paragraphs"
Should really be written as:
word_count = text.split.length
paragraph_count = text.split(/\n\n/).length
puts "#{word_count} words"
puts "#{paragraph_count} paragraphs"
Seems like doing it this way makes it easier to go back and edit code later, since the two won't be mixed together. I'm sure there are exceptions to this rule, however.
On a side note, the program I wrote scanned a text file and displayed information based on the text file. But it kept throwing in some junk characters, adding a "\92" after every sentence. So I went back through my code, trying to figure out what I'd messed up. The real culprit was the text I'd copied and pasted from the internet, and clearing the formatting of the text file got rid of the junk characters. Awesome!
I'd like to thank everyone who's given me advice or commented on my blog or otherwise encouraged me to learn Ruby. I'm having a great time so far. I think about what I know about Ruby today and how it was completely unfathomable to me a week ago, and then I think about what I'll know next week that is completely unfathomable to me right now and I can't help but get excited.
But here's a question. It seems like it makes sense to separate logic from output. So that this bit of code:
word_count = text.split.length
puts "#{word_count} words"
paragraph_count = text.split(/\n\n/).length
puts "#{paragraph_count} paragraphs"
Should really be written as:
word_count = text.split.length
paragraph_count = text.split(/\n\n/).length
puts "#{word_count} words"
puts "#{paragraph_count} paragraphs"
Seems like doing it this way makes it easier to go back and edit code later, since the two won't be mixed together. I'm sure there are exceptions to this rule, however.
On a side note, the program I wrote scanned a text file and displayed information based on the text file. But it kept throwing in some junk characters, adding a "\92" after every sentence. So I went back through my code, trying to figure out what I'd messed up. The real culprit was the text I'd copied and pasted from the internet, and clearing the formatting of the text file got rid of the junk characters. Awesome!
I'd like to thank everyone who's given me advice or commented on my blog or otherwise encouraged me to learn Ruby. I'm having a great time so far. I think about what I know about Ruby today and how it was completely unfathomable to me a week ago, and then I think about what I'll know next week that is completely unfathomable to me right now and I can't help but get excited.
Subscribe to:
Posts (Atom)