Posts Tagged ‘Ruby on Rails’

Learning Ruby

Friday, October 24th, 2008

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#

class Mammal
{
 Mammal()
 {
 }
}

Ruby

class Mammal
 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#

class Person: Mammal
{
 Person() : base()
 {
 }
}

Ruby

class Person < Mammal
 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#

class static PersonExtension //I always add extension, purely as a standard practice but isn’t required
{
 public static GetFullname(this Person person)
 {
  return string.Format(”{0} {1}”, person.Firstname, person.Lastname);
 }
}

use:

Console.WriteLine(person.GetFullname());

Ruby

class PersonModule #much like with my C# I can see this becoming a common standard for me.
 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.

Mitcheru and Mitchell Geere now one.

Friday, October 24th, 2008

So for a while I tried to have two persona’s the one “mitcheru.com” and the other “mitchellgeere.com”. I created “mitcheru” so that I could rant, or rave about things and “mitchellgeere” as a more professional blog relating to industry matters.

I have however decided that due to my lack of time to manage even one blog that I will amalgamate them back to one blog and keep it clean. Also joining the two gave me some material to blog about quickly. I know riveting stuff right.

Now in more news my aspirations to move to New York are still there and with such force, however I have come to realize that initially I may have to settle for Miami, Seattle, or somewhere in California. Neither one of the options are terrible at all in fact given the right job or opportunity they would be amazing! However if you have ever been to New York you will know what I mean nothing but nothing comes close to that city. I literally fell in love with the city.

The other things going on in my life is I am facing a cross road where I need to make a decision between sticking with Microsoft with their - in my opinion awesome - .net platform and the C# language and on the other hand Apple and coding then in Ruby and again the amazing Rails platform. I have had a long standing like/hate relationship with Microsoft, and a long standing love relationship with Apple. Learning a new language and platform isn’t really a big deal for me I have done it a million times, however I have always maintained working on Windows as my operating system. This will be a massive change for me because I have only ever done say a ratio 1:30 hours on a mac versus pc. So I decided that I will sink my teeth into rails – have already set myself up with all the material and my server can host rails already - and perhaps kick off one of my hobby projects in ruby on rails. All aboard!