Introduction
I am on my way to learn Ruby (my way). So I randomly try out things I think I might need or think that seem important to me. I try not to look at books to much because I learn best from making mistakes, if you do everything right the first time you probably have no idea what will happen if you do it wrong. I also try to write tests for everything and see what exceptions I get and why I get them.
My first class
So I’m still using Rubymine (I guess until my trail runs out and I have to go look for something else or if it’s good enough I might buy it)
So first thing I did was create new class file. I called the file plant.rb and the classname was plant. And I got this error.
That’s strange, what does it mean? It means I should have started the name of my class with a capital. None of my tutorials I read mentioned this little fact. But good to know. I guess it is time to read up on Ruby naming conventions.
So I changed the name and wrote a test.
```ruby require ‘rubygems’ gem ‘test-unit’ require “test/unit” require_relative “plant”
class Test_plant < Test::Unit::TestCase
def test_if_plant_has_name my_plant = Plant.new assert_equal(“test”, my_plant.get_name,“Plant has the wrong name”) end end``` And of course it fails with a NoMethodError.
So I create one.
ruby
class Plant
def get_name
# code here
end
end
and run the test again. The test still fails but for a good reason.
So lets make the test pass shall we.
ruby
class Plant
def get_name
return "test"
end
end
And it does pass.
Seems like we got the basics covered. Now I also want to test if I can set the name of my plant.
ruby
def test_if_name_can_be_set
my_plant = Plant.new
my_plant.set_name("new name")
assert_equal("new name",my_plant.get_name,"Plant has the wrong name")
end
I get the NoMethodError on the first run of course, so I add the method.
```ruby class Plant def get_name return “test” end
def set_name(name) # code here end end``` Run the tests and the first test still passes but the second test still fails. With this message.
Plant has the wrong name.<br /> <“new name”> expected but was<br /> <“test”>.
I guess it is time to add a local variable to me class.
Local variables in ruby are nice since you don’t have to declare them anywhere. The first use just declares them. Local variables also start with an @ sign. So I changed my class Plant to this.
```ruby class Plant def get_name return @name end
def set_name(name) @name = name end end``` Which now results in the first test not passing and the second test passing.
To mak the first test pass I need a constructor. In Ruby constructors are a method called initialize.
```ruby class Plant def initialize @name = “test” end
def get_name return @name end
def set_name(name) @name = name end end``` And now both tests pass.
Conclusion
There is a lot more to classes then I have here, but the above just described how coming from a .Net background I would look at it. I found all the things I have in .net and went with it. But of course that is the wrong way of looking at things since Ruby can do so much more with classes. But now that we got the basics covered and we know how to them, we can move on to other things. I found the pragmatic programmer’s guide to be a very interesting read fro more advanced things.