Wednesday, September 21, 2011

Instance variables and local variables in Rails

I've got a lot to learn about local and instance variables. I already knew 'character' meant a local instance of character, but I thought '@character' referred to another model, and not an instance of the Character model. I knew that sometimes if Rails was complaining about an unidentified local variable, putting an @ in front would often fix the problem, but I didn't know why. I think I've got a better handle on this, but I think the time has come to do some serious background reading to try and patch up some of the holes in what I know and what I think I know.

On the other hand, thanks to help from @jqr and a healthy does of trial and error, I've gotten that pesky statistic bonus problem figured out. This is what ended up working:

def total
  fortitude_base + character.statistic.con_modifier + magic + misc
end

And here's the con_modifier definition from my Statistic model:

def con_modifier
  (constitution.to_i - 10) / 2
end


I'm keeping it as .to_i now, as a fix for a case where a player tries to create a Fortitude save before a Constitution score has been entered. The .to_i translates a nil record to 0, so Rails does a temporary calculation (which isn't shown) rather than displaying an error. Not sure if there's a better way to handle this (perhaps simply checking that statistic.constitution.present? before performing any calculations) but it works for now.

More importantly than simply figuring this one instance of how to define logic in one model and then call it in the view of another is understanding HOW it works. I can extrapolate these methods and apply them to other things: skills, attack rolls, armor class, etc, etc. Once again, Manticore proves to be a useful learning tool! SUCCESS!

1 comment:

  1. The main thing to think about when dealing with the different variable types is scope.

    Local variables have limited scope and exist within the method, block, etc. they're defined within. Starting out, just think of them as variables that aren't associated with an object's state. They're the ones you'd use within methods to temporarily hold values while performing calculations or procedures.

    Instance variables are useful for maintaining object state. If you're holding a value within an instance for use by multiple methods or across multiple accesses, use one of these.

    You're probably dealing with these primarily in the context of controllers and views. When you make a request, it's routed to the appropriate controller. An instance of this controller is initialized and the appropriate action called. Within this action, you set instance variables which, through some Rails magic, are accessible within the view. Try not to let this Rails behavior confuse you as to the basic nature of the variable types.

    Straightforward documentation:
    http://www.rubyist.net/~slagell/ruby/localvars.html
    http://www.rubyist.net/~slagell/ruby/instancevars.html

    ReplyDelete