Where are Kyle and Matt?
The development of Radrails seems to have slowed to a crawl recently, which is a shame as I was eagerly awaiting features promised for version 0.8!
According to posts on the radrails community pages, it looks like the main members of the Radrails team are spending their time on other more profitable ventures.
All the same, for me Radrails is still the best value rails IDE. Some other IDEs have similar features but you have to pay for the privilege.
Where did the Radrails team go Has Radrails disappeared off the face of the earth
Pointers demystified 1
Over the last few months, my day-job has required that I do a bit of C+ development. Previous to this, the last time I did commercial C+ was in one of my first developer roles back in 2002, so I’ve recently been rediscovering it’s joys.
The thought of C+ often scares people, but that needn’t be the case. I was lucky enough to come to C# after learning C+ but the transition the other way shouldn’t be that difficult either. In fact, I would definitely recommend that C-sharpers get to know C++, if they don’t already, as it provides a greater understanding of C#’s inner workings.
Much of the syntax is similar, and one of few big differences is memory management (in the form of pointers). This post hopes to demystify pointers and thus hopefully remove one of the final barriers from people having a go at C++.
What the heck is a pointer anyway?
A pointer is just a variable that holds a memory address. (i.e. the location of a value in memory). They perform a similar function to reference-types in C#, which have a value on the stack which is just an address of somewhere on the heap.
Making a pointer
To make a pointer, you just declare variable of a certain type, but precede the name with an asterisk:
int *pMyPointer = NULL;(I just said that a pointer is a variable that holds a memory address, so you might be thinking: “Why do you need to give it a specific type?”. Well, the type you specify is the type of the variable that will be stored at the memory address to which the pointer “points”. This lets the compiler know how much space to reserve at that memory location e.g. 4 bytes for a 32 bit integer etc.)
Anyway, in the example above, I created a pointer which will point to an integer, and I gave it the value NULL. i.e. it points to nothing yet. (Note that NULL in C++ just means 0).
The opposite of a pointer
The opposite of a pointer, I suppose, is the address-of operator (&). This lets you get the memory address of a variable.int myCount = 132;
pMyPointer = &myCount;Assigning values at memory locations
You can assign to the location in memory to which you’re pointer points by using the syntax:*pMyPointer = 12;int myNumber;
myNumber = *pMyPointer;Heaping it up
In C++, when you use the ‘new’ keyword, it returns a memory address on the heap, and you need to assign it to a pointer. e.g. to assign enough space on the heap for an int, and put the memory address in a pointer called pYourPointer:int *pYourPointer = new int;*pYourPointer = 500;delete pYourPointer;MyObject *pPointy = new MyObject(1,3);
delete pPointy;
pPointy = new MyObject(4,5);After you call delete on an object, for safety’s sake you should always null-out the pointer to avoid leaving a pointer dangling, and accidentally using the deleted pointer again (which would cause your app to crash). Note that it’s always safe to call delete on a null pointer.
pPointy = NULL;~MyObject()
{
// do cleaning up here!
}Geting at your members
One of the weirdest things for me, coming back to C++ after years programming mainly C# was getting used the way you access members on objects on the heap again. Say you’ve got a pointer for a Person object, and the Person class had a GetNumberOfToes() method, you’d use the points-to operator (->) to call it. (Note that the familiar dot operator is used for objects on the stack). e.g.Person *pMyPerson = new Person();
int toes = pMyPerson->GetNumberOfToes();
delete pMyPerson;(*pMyPerson).GetNumberOfToes();Another use for the Ampersand… References
Having read all that, you might start poking round some C++ projects that you’ve got access to, but were previously too scared to open. For completeness, I thought that I would continue to explain another use for the & operator, so you don’t end up completely confused when you see it everywhere.
The & operator is also used for references. References are really just a way of giving an object another name. Anything done to the reference is done to the original. e.g.int myInt;
int &refToMyInt = myInt;refToMyInt = 930; //actually sets myInt to 930Passing by reference
This is a familiar concept to C# programmers, and is simple in C++ too. You just declare a function with the parameters that you want to pass by reference with preceding ampersands. e.g.int DoSomething(int &intOne, int &intTwo)
{
intOne = intOne + intTwo;
return intOne;
}int anInt = 1, anotherInt = 2;
DoSomething(anInt, anotherInt);Want more?
There’s loads more I could talk about here (I’m getting a bit carried away!), but I’d be here all night if I carried on. If you want to know more, buy a book!
Setting up rails on OS X (including radrails) 2
Hello. This post documents how to get rails going on Mac OS X (I’m using 10.4.8, by the way).
Well, basically I followed these brilliant instructions on Hivelogic.com (except I used ruby 1.8.5 instead of 1.8.4) and it just worked! The only little quirk was getting the lightTPD web server to work properly. I had to tweak the paths on the first line of the dispatch.cgi, dispatch.fcgi and dispatch.rb files (in the public folder of my rails project) as they were pointing to a windows-style path.
Radrails pretty much just worked “out of the box” too, but to get radrails to start the lightTPD server properly, I set up a symbolic link like this…ln -s /usr/local/sbin/lighttpd /usr/bin/lighttpdRemember this post from September where I explained how to setup debugging in radrails on Windows XP? Setting up debugging for radrails in OS X is almost exactly the same, but in the interpreter arguments page of the debug dialog you’ll want to put something like:
-I"/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.1/bin"Also, I had trouble getting debugging to work with lightTPD, so I’ve put the following in the program arguments to force it to use webrick instead.
webrick -p3001That’s it. enjoy!
UPDATES (March 07): There are now updated instructions on Hivelogic.com. You might also want to try Textmate and ruby-debug
when is private not private? 1
If a method within a class is marked as ‘private’, one might expect that it is only accessible from inside of that instance. Well, you’d be wrong (at least you would in C# – and I assume the rest of Microsoft.NET too).
In C#, if you define a member as private, then it is indeed accessible only to that class. However, it isn’t restricted to only that instance: Any instance of that type can access the private members of another instance of the same type. This is a bit counter intuitive to me, and freaked me out a bit when I first saw it, as it seems to break encapsulation! Try it if you don’t believe me.
Ruby has a different approach, and one which goes some way towards placating my OO-purist tendencies. As you’d expect, public methods can be called by anyone. Protected methods can be invoked only by objects of the defining class and its subclasses (ANY instance of the defining class). Private methods can only be called in the current object’s context. i.e. you can’t invoke another object’s private methods, no matter what it’s type.
ruby instance variables in derived classes
I thought I would tell the world about something in the ruby language which surprised me a bit. Anyone who has used ruby for any length of time will no doubt already know this, but it took me off guard a little, coming from a C++, java and C# grounding in OO.
First a bit of background for the uninitiated… In ruby an instance variable is one preceded by an @ sign and, as with all variables in ruby, you can create one simply by assigning to it.
Anyway, in ruby, instance variables are automatically accessible to any derived classes. This is especially pertinent in rails, when dealing with the (base) ApplicationController. If you add a before_filter method to the ApplicationController, which assigns some instance variables, they are magically available in the derived controllers, without having to add accessor attributes to the base class.
Here is a very simple example…class Parent
def parent_meth
@var = 'hello'
end
end
class Child < Parent
def child_meth
@var
end
end>> child = Child.new
=> #<Child:0x2430560>
>> child.parent_meth
=> "hello"
>> child.child_meth
=> "hello"I hope this post helps prevent some chin-scratching for other ruby newbies.
(If you’re intersted in reading more about this topic, here’s a discussion I started in the ruby forum).
I'm Richard Roberts, a developer in the UK working with Ruby on Rails. I'm a founder of: 
