small medium large xlarge

Generic-user-small
24 Oct 2008, 18:09
Alan Slater (4 posts)

Hi, this is a question and possibly also a suggestion for vIII based on trying and (so far) failing to learn rails from books (and lots of them).

Something I’ve really wanted but not yet found is a clear explanation of instance variables in Rails - something that will help me visualise the some of their most common functions in Rails applications and the basic essentials on how they work, so that I can start experimenting with them confidently.

To clarify, not an explanation of instance variables in Ruby, but an explanation of instance variables in Rails.

That might sound stupid, since obviously they’re the same thing. There are differences though. Partly this difference is from the in-built Rails features such as changes to where they can be accessed from and pre-packaged methods that use them. For some reason, even in Rails books, descriptions of instance variables don’t mention these. In addition, it’s from differences in convention and the kinds of functions they’re normally used for - this is particularly important for us newbies trying to visualise what things do enough to know how to start experimenting.

To give one example, I’m finding it very hard to square what I see in example Rails applications of instance variables being used to carry and manipulate arrays of data off a table with what I read about Ruby’s instance variables being like attributes of objects intended mainly for internal storage. For example, in a very standard use of find_all to get the contents of a table up through a controller into the view, are the table contents put into a single array in a single instance variable which acts as a single object, or does it creates a stack of instances each with one array to which the instance variable is the class, or does it create a stack of instances of the controller class each with one instance of the instance variable and one array…

Clearly I’ve understood something very badly wrong, and from browsing forums and blogs I’m sure I’m not alone. Descriptions of instance variables in pure ruby have just confused me further. When I’m more comfortable with the basics, pure ruby will be great to broaden my horizons as to the range of things it’s capable of, but right now they’re confusing. I feel like I need to understand the standard, basic ways instance variables are used and work in rails first, and then make it more abstract when I’ve got a firm handle on the basics, if that makes sense.

Is this just me? And can anyone see where I’m going wrong? All help appreciated, thanks, and apologies for any embarrassing gaps in my knowledge.

Generic-user-small
26 Oct 2008, 16:18
James West (104 posts)

You’re not on your own.

I am still struggling with where to put things and how variables get passed around There is so much to rails that it is difficult to see the wood for the trees and I’m still waiting for that “eureka moment” or an epiphany where I suddenly see the light.

I feel as though a pea soup fog has lifted and been replaced with an early morning mist but I can still only see a few feet in front of me.

I really want to see some kind of easy to visualise explanation of how to pass variables around and what to do with them.

I’m starting to get there though with a little help from peeps in the rails forums.

It’s stupid things like how to get an object out of a shared partial, pass it back to the view, then have the views controller take that object and pass it to a shared class in a lib folder for further processing.

I’m going through a step by step walkthrough with a thread running on the rails forum that is helping me to understand what should go where and how it should be used. If you phrase the posts properly then people are really helpful and can understand what it is you are trying to get your head round.

There is enough info in the book to find out how to do things but it is presented in a way that makes it very difficult to get the basics as you say.

Trying to piece it all together is very difficult especially when you start talking about dealing with information that is not stored in a database.

Dave_gnome_head_isolated_pragsmall
26 Oct 2008, 19:06
Dave Thomas (390 posts)

It’s stupid things like how to get an object out of a shared partial, pass it back to the view, then have the views controller take that object and pass it to a shared class in a lib folder for further processing.

I think you’re trying to think about this too hard. You don’t do anything for this to work. All the partial does is lay stuff down in the browser. Once there, no one (including the browser) knows how it got there—it’s all just HTML. So stuff entered into a field that you happened to generate using a partial is simply passed back to the controller in the next request just the same way everything is passed back. No partial-special magic at all.

Dave

Generic-user-small
26 Oct 2008, 21:19
James West (104 posts)

There you go you see. Told you I was being really stupid :-) Thank you Dave.

Generic-user-small
28 Oct 2008, 12:54
Alan Slater (4 posts)

Yeah, I agree with the visualizing thing. I get the impression that there’s quite a lot of people who are coming to Ruby (& Rails) as a first programming language and who are more used to thinking visually than thinking abstractly. It’s certainly true of me, I’m guessing it’s true of you too James? I also get the impression that there’s a lot of tiny little things which we don’t quite get that seem obvious to experienced programmers. Maybe there’ll be space in the 3rd edition for a single page titled “For you fools who jump straight into Rails with little or no real programming experience…” that lists our common mistakes… and I’d be more than happy to help suggest a few!

One big breakthrough I had was realizing that Rails is the sort of thing you learn by doing first and then come to understand later (like riding a bike or learning to speak a language or play an instrument), not the sort of thing you understand first and then do (like HTML & CSS, studying or any computer application with a GUI). I’d been trying to understand the principles first then get better at it by making applications when I felt like I knew what I was doing, what I should have been doing was making loads of little dummy applications all the time without really expecting them to work and learning by trial and error from that. That probably sounds obvious to anyone who’s learnt a full programming language before, but it was a big surprise to me.

Another tip that’s been really useful to me is about error messages. There’s a brilliant thing on this in Chris Pine’s ‘Learn to Program’ (great book by the way, I recommend buying the pdf from this site), saying to think of error messages not as an aggressive ‘COMPUTER SAYS NO’ but as a confused friend clumsily trying to ask for help or clarification. I think error-phobia is a big part of the reason why newbies like me struggle - we don’t like to experiment because we don’t like getting errors, because we’re used to the kind of bizarre and seemingly meaningless error messages that come from bugs in professional applications and so we don’t yet understand what they’re really there for.

So, yeah, don’t think too much about the nuts and bolts themselves, just play with them and vary things in small ways until it feels more comfortable. If it works, brilliant, if it doesn’t, no loss.

Anyway, I’m off to experiment with instance variables. If you have any breakthroughs James, I’d love to hear them.

Generic-user-small
28 Oct 2008, 22:30
James West (104 posts)

Alan I have a shed load of development experience (19 years this week) but no HTML, Ruby or Rails experience. I have used 8 languages in anger and am familiar with 5 others and touched on a few more.

This language will be my 9th language to use in anger and I’m still struggling. My learning curve is no where near as good as it used to be which doesn’t help. I have seen so much of how things work when dealing with the generators but actually coding is really difficult because of the way things are laid out in pretty much all the resources I’ve looked at.

The answers are in this book which is great but I’m having a heck of a job knowing how to find them.

The trouble is that I KNOW what it is that I want to do but finding the Rails way to do it is very difficult.

Personally I would have picked a different language if I wanted to learn programming but it is the route you have chosen and at least you are being exposed to a true OO environment.

Finding the Rails Way would be a good book.

Where does everything fit? What do Routes do for you? The problem is that there are always so many different ways to do something in an OO language and there is no right way and no wrong way to write programmes. If it works then it is the right way but there are better and worse ways and there or good and bad ways if you prefer. I have seen many pointers as to what those better ways are in the book but I have not enough info yet to make it all fit.

The Depot app example is not the way an experienced developer would write an app. It uses far too much of the auto generated code etc… But it is a good example of how to get started.

It has just taken me a week to find out how to write a class properly (I think) but the mist is lifting slowly but it took some digging to realise that what I actually needed to know was the bit on page 665 section A.4 Classes.

Having read that section I find myself needing to know more.

It’s the nuts and bolts I am interested in. Such as Do I write a partial or do I write a helper or do I write a custom form builder thingy (which from the book it seems is just a glorified helper anyway) in order to have a reusable file upload bit of code to place in my view and then how do I write a bit of generic functionality and where does it go?

I’m getting there but I’m not there yet. I suspect that when I have found answers to my questions I will have a better way of doing things but it won’t be the best way.

Maybe there should be a forum somewhere for newbies to rails to help out each other.

Alan, I think we have the same problem but from different ends of the spectrum and that tells me there is definitely something missing somewhere.

Maybe what I should be learning is Ruby first then learning the framework of Rails after but I don’t have time for that.

But maybe that’s the answer. An epiphany of sorts has just occured. It would be good for you to learn the language then worry about the framework of Rails.

This book is really really good but I think that if I had Ruby and HTML knowledge first it would be even better for me and possibly for yourself Alan.

I don’t think any book can teach you how to program. That is something you learn from experience. It’s a very creative process and everyone has their own style and quirks, and you build up your own style and the way you program evolves as you move on but learning how a language works is paramount. Much the same way that 2 different artists will approoach painting the same picture. You will get different results but you will get a picture.

I guess you have to know what it is you are wanting to achieve before you can achieve it and if you want any pointers then ask. I may not know the rails code but I could certainly help point you in the right direction.

If anyone can recommend a good Ruby book I would love to tead it :-)

Generic-user-small
29 Oct 2008, 01:59
Alan Slater (4 posts)

Sorry, I didn’t mean any offence, I should have read your other posts in other discussions first.

A couple of books I’ve seen rated highly that sound like what you want (you almost certainly already know about them so sorry in advance, but just in case) are the pickaxe book for Ruby (Programming Ruby by pragmatic programmers), and ‘The Rails Way’ by Obie Fernandez which claims to be everything an experienced programmer needs to understand the rails conventions and framework.

One other possibly useful suggestion would be take a step back and look at the bigger picture. Make the most of how forgiving and convention-driven Rails (apparently) is compared to other languages. It’s good to be able to think in functions first and get a rough overall sketch of what does what and where, and then bring in the detail slowly and gradually.

Try putting the computer away for an hour or two and sketch boxes and arrows on paper without using a single piece of jargon or code - only the functions each part does and how they interact in plain English. Just think in views people interact with, controllers that do commands for and from the views, models that crunch and validate classes of data for and from the tables and tables that store instances. No detail beyond rows on a table and things that could appear on a web page. Then and only then worry about details like how and where best to chop the view up into partials, when to use forms people type into and when to use fixed links based on URLs to get parameters and input to the controllers, which bits of code are likely to be so complex or common that it’d be easier to cut them out and keep them in a helper, which functions to take right down to the model, etc etc.

It probably sounds silly but I’ve been trying to with model Rails applications using stacks of playing cards to represent tables, sat on large upside-down post-it notes to represent their models, with big chalk arrows on the desk connecting them with controllers and controllers with, for the views, sketches of what the actual pages would look like in HTML/CSS (A5 half-sheet of paper for each layout, post-its stuck in the appropriate place for each partial). Obviously it’s crude but it’s good to be able to see it all at once and to physically move things around, adding layers of detail one at a time. It was doing this, trying to work out how the controllers would work together that made me realise I needed a better understanding of instance variables (no luck there just yet, still trying).

Anyway, hope there was something useful (or at least curious…) in there somewhere.

Generic-user-small
29 Oct 2008, 12:55
James West (104 posts)

Hi Alan. No offence was taken :-) I just found it interesting that 2 people from different ends of the spectrum as far as development is concerned were having similar issues learning Rails.

As it happens after my last post I wnet looking for books and the pickaxe book is one that I am considering quite seriously. It’s in BETA at the moment and I am hooked on this BETA thing with this book. Much better to get your info as up to date as possible when starting out ne thinks!

I am obviously trying to understand similar issues and I think I have an incling into the bit of insight that you are missing and like all things the complexity of usage may be hiding the simplicity of the language (wood and trees). After my last post I have gone hunting for more info both in the book and elsewhere and as far as instance variables are concerned I think I have it now.

Given that Rails is a framework and not a language (Ruby is the language) you can learn a lot on how to use Ruby by looking at what Rails does with it in order to provide the tools and functionality that you get with rails. This is inevitably going to provide you with a second language as after all you are using commands that have been created using Ruby and those commands are obviously not going to be Ruby commands else Rails would already exist.

Please forgive me here as I am thinking as I type. Instance variables. Given the pure OO nature of ruby it means that all variables are classes. Hence strings have manipulation functionality.

Given that ALL variables are classes then, all instances of classes are objects (an object is an instance of a class that you use) ergo all instance variables are objects. Now it’s a case of understanding what you can do with those objects and how to pass them around.

If I am telling you something you already know then sorry but this is helping me. Having the ability to chuck one object around to various methods is fine but quite limiting so we come to ways of associating objects with other objects (or instance variables if you prefer) and from reading the book this can be done in one of 2 ways by using collectios. Collections come in the form of either a hash or an array. (An epiphany has just occured but I’ll come back to it :-)) A hash is a way of holding together groups of varying types of objects or instance variables and has a name in order to reference it. You navigate this hash with loops and you can access specific objects by name using the :whatever syntax. These hashes are very flexible and have great usage potential for usage such as passing in optional parameters to a method or function call.

Question - Are all methods functions? That is to say do all methods have the ability to return a value? I think so!

An array is far more efficient than a hash (It sais so in the book) but not as flexible (Epihany moment mentioned earlier - Go and gen up on hashs’ and arrays)

OK understnading this then learning rails is going to be just a case of learning the functionality that is available. i.e. getting know the common clases that you use and what methods and properties they have plus the syntax for cucking these variables around.

If I now go back and look at the way the products were looped through to place them on rthe screen in the Depot app I will know what collection of objects or instance variables have been generated to wrap up the data from the database. Whether those objexts are prepopulated at time of fetching from the datanse or whther they are populated using an internal array at time of usage I don’t need to know at the moment so I don’t really care. I just need to accept that they are there and can be used. I suspect this will become important later when considering performance issues for bottlenecks.

Now I’m going to go away and gen up on scope for classes as I think that a protected method or property is the equivalent to a friend in V.B.

I also need to really understand the way that the framework (or Rails) behaves and I suspect that routes may be key to this. So more info is needed there. I am sorry if this is just old ground for you but it has helped me a lot.

I often find that no amount of drawing or reading can substitute for talking a problem through.

Generic-user-small
29 Oct 2008, 12:59
James West (104 posts)

Alan. I have a feeling that we could probably help each other out quite a lot if we were able to talk. I’m not posting a public contact in this forum so if you have any suggestions then let me know.

James

Generic-user-small
29 Oct 2008, 13:42
James West (104 posts)

Alan. I have a discussion running on the Aptana cloud forum here http://forums.aptana.com/viewtopic.php?t=6860 It’s my OP so you could PM me (There is a link for Private Message) and we could sort out the details from there if you wish.

James

Generic-user-small
29 Oct 2008, 14:03
James West (104 posts)

In my hunt to find out how to write classes properly I cam accross this

http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html.

Totally awesome. Alan - It’s well wort a read.

You must be logged in to comment