You are not logged in.
Pages: 1
Hi!
I have followed a Ruby tutorial on the Internet, and one of the last excersises was to write an OrangeTree class in Ruby. I have done that, but would like you to give me some critic on the code.
Is there anything I can do in a better way?
// Kris
"Penquins, Penquins, über alles!"
Offline
In Ruby we write method and variable names like_this instead of likeThis.
I'd write show_height() instead of just height to indicate that it's a method call and that we're interested in the action it performs and not in its return value.
Ruby supports @age += 1. Also @orange_count -= 1.
You can use string interpolation: "There are #{@orange_count} oranges on this tree."
Because @height is only used locally, you can use a local variable.
You can use a case statement instead of if-then-elseif-else.
Here's the new version:
class OrangeTree
def initialize
@age = 1
@oranges = 0
puts "The Orange Tree".center(40)
show_height()
end
def one_year_passes
@age += 1
show_height()
if @age >= 100
puts "Your tree dies..."
exit
elsif @age >= 3
produceOranges
end
end
def count_oranges
puts case @oranges
when 0: "There are no oranges on this tree."
when 1: "There is one orange on this tree."
else "There are #{@oranges} oranges on this tree."
end
end
def pick_an_orange
if @oranges >= 1
@oranges -= 1
puts "You pick an orange..."
puts "The orange is big and sweet."
else
puts "There are no more oranges to pick this year."
end
end
private
def show_height
height = 10 * @age
puts "The tree is #{height} cm high."
end
def produce_oranges
@oranges = 2 * (@age-1)
end
end
Offline
Thank you for the help
So:
class-names are written like this: ClassName
function-names are written like this: function_name
variable-names are written like this: variableName
Or, am I wrong?
// Kris
"Penquins, Penquins, über alles!"
Offline
That's right except variable names like variable_name.
Offline
Okay, thanks Do you know some good guides/docs for ruby? which is understandable?
// Kris
"Penquins, Penquins, über alles!"
Offline
I don't know any, but that doesn't mean they don't exist. I like to learn by doing: write small programs. You can try to find information on the internet, maybe other people are trying to do a similar thing. And you can always ask (here for example) if you get stuck.
If you'd like to read a complete guide then your best option is buying a book. There's an online book (http://www.ruby-doc.org/docs/ProgrammingRuby/) but it's quite old.
Offline
@kris
What tutorial did you follow? I'm also interested in learning Ruby, I just now made my first steps into the rubyworld with http://pine.fm/LearnToProgram/ It might be to easy for most folks though, it's aimed at people new to programming (which I am).
freetechbooks.com also has some books on Ruby (which I didn't try yet): http://www.freetechbooks.com/ruby-f49.html
For lack of better words: chair, never, toothbrush, really. Ohw, and fish!
Offline
Mr. Neighborly's Humble Little Ruby Book is mentioned on the freetechbooks website. I forgot about it. This book is very good. http://www.infoq.com/minibooks/ruby
If you're new to programming I recommend Hackety Hack. I think there is a Linux version too now. http://hacketyhack.net/
Offline
Pages: 1