So, below you can see a simple class written in Ruby:
And an object instantiation:
m = MyClass.new
Adding methods is very easy too:
puts "That's a Ruby method!";
If you're using irb (interactive ruby), a tool to execute interactively ruby expressions read from stdin, you don't have to re-instantiate 'm', the changes made in MyClass will affect 'm' dynamically (on the fly), so if you call 'm.my_method', don't worry, it'll work.
If you're not sure about a method in a object, you can do this:
if m.respond_to? "my_method"
Do I need to explain something else?
The 'initialize' method represents a constructor in a Ruby class and the parameters come after:
def initialize name = "Hello World!"
@name = name
The '@name' is a field, like 'private Object obj;' in Java, and in the case above 'name' has "Hello World!" as its default value.
If you try to call 'm.name' you'll get something like:
NoMethodError: undefined method `name' for ...
This is because name is 'private', to create 'getters' and 'setters' you have to add 'attr_accessor :name' before the constructor. So, after that you can call 'm.name' or 'm.name = "hello"', for example, as you wish.