Archive for April, 2008

Ironruby.2.1 – Fundamentals (Class – End)

April 28th, 2008 by 

In this blog, I will wrap up the basic class concept in Ruby. So far what we have see is, how to define a class, how to create methods and how to call them. How to define instance variable (class variables) and how to access them in the code through the use of methods. Defining a method with instance variable is cool when you need to manipulate them to give a different answer. Say if you do not want to do anything with the instance variable but would like a way to access them with one or two line would be awesome, don’t you think? Well, ruby has the answer for you as well. In this final examples of the basic class, I will show you to get hold of instance variable and use them outside the class definition.

Following code is created as ‘classs7.rb’

class Person
attr :name

def initialize(name, age, sex)
@name = name
@age = age
@sex = sex
end
end

p = Person.new(“Joe”, 30, “Male”)
puts p.name

(more…)

Ironruby.2.1 – Fundamentals (Class) Cont…

April 25th, 2008 by 

From the previous example, you noticed, all the variables declared inside the code is by default is private. If you want to access the class variables either you need to declare the getter/setter methods inside the class or make the class variable as public.

Lets look at the first approach or creating a getter/setter for a class variable.

class Person
def name #get method
@name
end

def initialize(name, age, sex)
@name = name
@age = age
@sex = sex
end

def sayName
puts @name
end
end

student = Person.new(“Joe”,30,”Male”)
puts student.name

In the above example the first part of the program, there is a method called name, which in turn return the private variable name. If you look at the last line of the code what we are doing is printing out the class variable itself to print the name, instead of calling the ‘sayName’ method.

I created the above code as class3.rb in my favorite notepad++ editor and fireup rbx and see what happens. Here is the result of the run;

(more…)

Ironruby.2.1 – Fundamentals (Class) Cont…

April 24th, 2008 by 

In the previous post we saw how to create a simple class and how to access the class variables inside the class methods. In this example, we are going to see how to get or set the values of the object variables and what are the limitations you need to know.

Following code is exactly same as the last one but in this we will try to access the object variable from outside the class and see what happens;

class Person
def initialize(name, age, sex)
@name = name
@age = age
@sex = sex
end

def sayName
puts @name
end
end

student = Person.new(“Joe”,30,”Male”)
student.name = “John” #in this declaration name is defined private
student.sayName()

[As I mentioned before, I use Notepad++ to write my ruby code. I type in this code and save it as class2.rb and I use this file to run the ruby code] Now we tell rbx to run our class2.rb code and the output will look like the following; (more…)

Ironruby.2.1 – Fundamentals (Class)

April 24th, 2008 by 

As you know by Ironruby is Ruby on DLR. So if you want to learn to progra in Ironruby, you need to learn to program in Ruby first. There are plenty of resources available online to learn Ruby. Most of the time, I do not rework the same subject again to save time but in this case since we are learning it together I will go over the language with you as I learn so we can learn it together. But I would encourage you to google ruby and learn more.

So in this installment, I am planing to go over classes and how to use them in Ruby with six examples. Most importantly I take it, you know what is Object Oriented Programming(OOP). If not ,please refer to wiki for that. If I have time, may be I will come around to write that as well. but for time being, I am going to go out on a limb and assume you know OOP. If not, please leave a comment and I will surely address it.

Basic class:
We are going to create a Person class, where person has a name, age and sex. If I use this class, I should be able say the person’s name as well. So here is how we do it in ruby

class Person
def initialize(name, age, sex)
@name = name
@age = age
@sex = sex
end

def sayName
puts @name
end
end

student = Person.new(”Joe”,30,”Male”)
student.sayName()

In the above example code all the bolded words means they are reserved words in ruby. Lets walk the code and see if we can understand what it is trying to do. Most of the time the ruby code is very easy to understand if you just read it alound (to yourself).

(more…)

Ironruby.2 – Findamentals (Ruby with C#)

April 24th, 2008 by 

If you are going to write programs in Ironruby you need to learn to write programs in Ruby as Ironruby is DLR implementation to Ruby itself. So here are some of the fundamentals we need to understand before we start working on Rails, Silverlight and of course WPF.

Some of the basic programming things we need to understand here is to how to declare and use some variables and classes.

Variables:

Lets look at variables, if you have worked with C#3.0 var then you have a good head start. In ruby you do not declare variable types, what I mean by that is, you do not specifically say, I want to declare a integer variable X, instead you just assign a integer value to a variable and ruby infers the type based on the value assigned to it and treat the variable as such in the future. This is like using ‘var’ type in c# 3.0. The main difference between var and ruby declaration is that, in C#3.0 you have to prefix the variable with var type to make C# understand what type of value it is going to use. So much of talking and lets look at an example

We are going to declare a integer variable x and assign the initial value of 5 and then add 1 to it and then print the value of x and of course it will print 6. First lets look at it in C#. We are only looking at the meat of the code. You need to little bit more to make this one run in C# as you all know.

(more…)

Ironruby.1.1 – Vista Begining

April 24th, 2008 by 

I was installing Ironruby on a Vista box, so I thought I will chat down all things I did for someone who might have to do the same thing.

1. Download and install Microsoft Visual C# from http://www.microsoft.com/express/download/#webInstall, It is a very easy to install, simply follow the setup wizard and choose the defaults in all the screen, except in my case I didn’t choose MSDN since it is almost 300MB and it takes a while to download. Also I can get most of all the documents for C# on the net, so to save time and space, I decided not to install. Please be patient while it download and install. It takes some time.

ok, I am back from lunch and guess what I finished installing and asked me to reboot my box. It may be because of the windows update. They always sneek in some updates :)

(more…)

Ironruby.1 – The Begining

April 24th, 2008 by 

Finally as promised here is first installment of Ironruby. In this, we will see how to install and run our first “Hello World” program. Before we start Ironruby I would like point you to some of ‘must have links’ for iron ruby developers. Here they are

1. Ironruby web site. This site has all the information for you to get started.

2. Jon Lam’s IUnkown web site. He is the Iron Ruby. His blogs are always worth a read so you must check out his blog at this web site.

3. Ironruby user forum.

4. Scott occasionally writes about Ironruby but his blogs are also good read for techies. One another blog you need to checkout while you are there would be Scott Hanselman. His blogs are also very absorbing.

Now that is out of the way, lets get started.

1. Download and install Ruby using ruby one click installer.

2. Download and install Tortoise SVN. After this you need to reboot the computer.

3. Now take a look at the “Gettting started” video by Jon Lam himself. Which has video instruction on how to checkout the latest code from Rubyforge follow that and checkout all the code to your c:\svn folder. If you do not want to watch it, then open a windows explorer and go to c:\ drive. Now right click on an empty screen where you will get a screen like the following

(more…)