You are not logged in.

#1 2008-07-05 12:56:47

Kris
Member
From: Denmark
Registered: 2006-07-11
Posts: 86
Website

The OrangeTree Ruby Class

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.

http://pastebin.ca/1062982

Is there anything I can do in a better way?


// Kris

"Penquins, Penquins, über alles!"

Offline

#2 2008-07-06 13:49:35

JulesJacobs
Member
Registered: 2006-08-16
Posts: 29

Re: The OrangeTree Ruby Class

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

#3 2008-07-06 14:21:29

Kris
Member
From: Denmark
Registered: 2006-07-11
Posts: 86
Website

Re: The OrangeTree Ruby Class

Thank you for the help smile
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

#4 2008-07-09 13:55:17

JulesJacobs
Member
Registered: 2006-08-16
Posts: 29

Re: The OrangeTree Ruby Class

That's right except variable names like variable_name.

Offline

#5 2008-07-09 23:20:42

Kris
Member
From: Denmark
Registered: 2006-07-11
Posts: 86
Website

Re: The OrangeTree Ruby Class

Okay, thanks smile Do you know some good guides/docs for ruby? which is understandable?


// Kris

"Penquins, Penquins, über alles!"

Offline

#6 2008-07-10 23:18:08

JulesJacobs
Member
Registered: 2006-08-16
Posts: 29

Re: The OrangeTree Ruby Class

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

#7 2008-07-11 17:55:20

lldmer
Member
From: Amsterdam
Registered: 2008-05-17
Posts: 119

Re: The OrangeTree Ruby Class

@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

#8 2008-07-11 23:13:59

JulesJacobs
Member
Registered: 2006-08-16
Posts: 29

Re: The OrangeTree Ruby Class

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

Board footer

Powered by FluxBB