Okay so this, so far, has been an interesting experience for me. Some are a mixture of ways that I have done things before and others completely new. So I am just going to try and show some basics in ruby and then its counter parts in C#.
The first thing I will do is show you classes from the basic declaration and then build it out from there.
C#
{
Mammal()
{
}
}
Ruby
def initialize()
end
end
Simple right all looks pretty much the same so far. Next we want to show inheritance, but I am really just interested in showing how the constructors will pass the data up to the base or super class. A note as with C#, Ruby only allows single inheritance.
C#
{
Person() : base()
{
}
}
Ruby
def initialize()
super()
end
end
The next thing I am going to show is what we know in C# as extension methods which are Modules in Ruby. I’m not sure if it is in fact the counter part to extension methods however it certainly looks like the closest thing to it. They also have what is called a singleton, don’t confuse it with pattern I will just show an intro to that too as an alternative - at least in my mind it is an alternative. Right so you declare a module and extension in the following way. I will assume we have attributes and properties respectively defined.
C#
{
public static GetFullname(this Person person)
{
return string.Format(”{0} {1}”, person.Firstname, person.Lastname);
}
}
use:
Console.WriteLine(person.GetFullname());
Ruby
def fullname
@fullname = “#{firstname} #{lastname} ”
end
end
use: #notice that in my example this is on an object/instance level. like I said I am new to this so there could be more to it
@person.extend PersonModule
print @person.fullname
alternative:
#@person already exists.
class << person
def fullname
@fullname = “#{firstname} #{lastname} ”
end
end
print @person.fullname
Again you can still see the similarities and you are happy. Now the next thing would what they deem as class level variables and instance level variables. Class level in C# would be static and the other would be the same. Where in C# you would declare it as say “public static string Variable” the static keyword denoting it as such in Ruby you would you double roses like this @@Variable. Instance level variables are declared with a single rose, however they are only instance level if within a method definition. If it is declared in the class it will be seen technically as a static or class level definition.
Again like I said I am really just informally chatting here I’m not trying to sound like an expert in either languages, although the amount of time I have spent programming in C# I technically am, however it was just interesting to see the differences and I will try and document more interesting stuff next time. For now I will just forge on, its only been an hour so don’t judge me just yet. Ruby is pretty different to C# they have a mantra that Class is an object, and Object is a class. Everything is an object basically, however I read an interesting thing that only a Class can have methods though. Class in the inheritence of Object specializes it into having methods/members.