Topic: Ruby Basics Question...

Hi before I tackle Rails I am working my way through the Manning "Ruby For Rails" book so I can grasp OOP basics and know what is going on in my apps.

Although the book is excellent I am trying to understand if I have this simple basic jargon correct.

if you have :

obj =

does obj = Object name , Object = Class name and new = method call with the "." being the operator to "call" the method?

If anyone can break it down better or give me any better explanation links would be appreciated.


Last edited by Opel (2007-01-05 12:02:06)

Re: Ruby Basics Question...

Perhaps it would help if I tried a more specific example...

Say I have a User.  The User is an entity on my system. I'll use a Class to represent that user. The Class will be the blueprint that describes things about the user.

Since each User is different, I can't just use the same Class for every user. I need to make a copy of it that is specific to my User... this is called an Instance.

When you do user =, you're creating a new instance of the User object. This is often referred to as a constructor. (the 'new' method does a bunch of things behind the scenes to prepare the class for later use.)

However, that example is kind of confusing, so let's make it easier to follow. How about
  homer =
  bart =

both 'homer' and 'bart' are variables that now represent object instances.  Assuming that a user had an email address, we would now access that from our variables.

   email =

So to answer your question...

   obj =
   obj    = variable name and representation of the object instance
   =      = Assignment operator
   Object = class name
   .      = operator for calling a method in Ruby
   new    = Class method for Object which initializes a new instance and returns that instance

Good luck!

Re: Ruby Basics Question...

You're trying to tackle some seriously heavy object oriented programming by making rails your first endeavor. I recommend stepping back and getting the basics down first. Apple used to have a fantastic introduction to OOP on their site using Objective C, but I can't find it anymore. Bruce Eckel's "Thinking in Java" may be another good starting point.

I've been professionally programming for 5 years and hobby programming all my life and there are still things about Rails that stump me. Rails is a strange beast in that it's both very easy and very hard at the same time.

Re: Ruby Basics Question...

bpHogan thanks for your explanation that cleared up a few queries that I had.

tortoise I am trying to tackle Ruby basics before I look at look into using it with Rails. I have looked at OOP in Flash and in some PHP cases. I have to tackle it sometime if I want to get anywhere with programming so why not Ruby & Rails?

Re: Ruby Basics Question...

If your interested in OOP and Programing Patterns I recommend the following book:

Re: Ruby Basics Question...

Thanks OmenKing, I skimmed the sample champter PDF and is a better way of explaining things, although the book seems to be for Java?

I'll have a read of it fully over the weekend.

How much knowledge is required of OOP for rails, I reckon a lot as the focus is on being a DRY language.

Re: Ruby Basics Question...

A solid understanding of OOP is really helpful with Rails - I know it has helped me. But, I wouldn't say it is absolutely necessary. If you grasp the basic concept of Classes and Objects, and how there are instance variables, class variables, instance methods and class methods, then that may be enough. No need to delve into OOP design patterns, although they are helpful, I find Rails has its own set of patterns.

If you are struggling learning Rails, take some time out and learn Ruby on its own. The concept that "everything is an object" can be quite a twist from other programming languages.

Railscasts - Free Ruby on Rails Screencasts

Re: Ruby Basics Question...

@ryanb has some great advice.

Many people I've trained (PHP, ASP, CF to Rails) have had no OO exposure. You can be successful with Rails and learn OO as you go as long as you have a basic understanding of the following things:

1. Classes and Objects
2. Classes inheriting from other objects
3. Class methods vs instance methods
4. Class variables vs instance variables

Also, you need to understand the MVC design pattern.... but in a nutshell
User requests something from the server. The request goes to a Controller, which determines what to do with the request. If the request was for information (search, database lookup) then the controller will call methods on various Models. These methods in the Models connect to data sources and /or execute business logic.  The Controller receives the data and then renders a response to the user by using a View.

  * models = business logic only. No access to request or session (they stand alone)
  * controllers = handle requests, interacts with the models, and route responses to views - NO BUSINESS LOGIC
  * views = the screens seen by the user. Keep code as far away from these as possible.  (For Rails, use partials and helpers to achieve this goal.)

Good luck to you!

Re: Ruby Basics Question...

Thanks the Slash 7 article helped me understand MVC and I think the explanations in the "Ruby for Rails" will be enough for me to begin with.

Re: Ruby Basics Question...

This is a great list:
1. Classes and Objects
2. Classes inheriting from other objects
3. Class methods vs instance methods
4. Class variables vs instance variables

I'm hoping someone could write an article or give an explanation of the way the above mentioned points work with Rails, and especially in relation to the MVC. It would be a ground-breaking article because with the rails community as a whole, no one seems to care about explaining OOP and the way it works for RoR. Like, how does class inheritance work, where do class methods go, what's the difference between class vars and instance vars, etc etc. That is all too important when it comes to understanding the inner workings of your app.

Last edited by maestro (2007-01-05 18:18:51)

Re: Ruby Basics Question...

Maestro all this is covered in the Manning "Ruby for Rails" book, 12 chapters of it with a final walk thorugh of a Ruby on Rails app that puts the theory to practice

Re: Ruby Basics Question...

try this extract from the Pragmattic Programmers site too :