Testing ... with console

I'm still new leas than 40 hours with rails, and while I've been following the discussion about testing I'm still further back along the line, working my way though AWDwR and trying variations.

One thing I noted is that functions - and I presume variables/accessors - that are defined in app/controllers/application.rb will be available throughout the application. (Presumably, unless they are over-ridden)

I'm just playing with this but its not doing what I expect.

I'm defining some regular expressions as well as functions that use them

   @regex = {           'upperalpha' => /[A-Z]/,           'loweralpha' => /[a-z]/,           'numeric' => /[0-9]/           'upperalphanumeric' => /[A-Z0-9]/,           'loweralphanumeric' => /[a-z0-9]/,           'alphanumeric' => /[A-Za-z0-9]/    }

   def isanumber?(string)        /^#{@regex['numeric']}$/o.match(name)    end

So, times comes to test this in a very basic way and I fire up the console, iterate through my typographic errors and ...

Well, I don't seem be able to find @regex or isanumber?

I tried adding read_accessor, I tried accessing Application::regex all to no avail.

I've tried putting the regex definition in an initializer.

I know that the console script *is* reading app/controllers/application.rb because it blew up on some of my typos!

It here some special conditions about the console?

I can access the database objects OK.

You need to create an instance of the ApplicationController class (or any other controller) and use that to access the instance methods you have defined. Something like this

a = ApplicationController.new

=> #<ApplicationController:0x3229734>

a.isanumber?(5)

=> true

The console is great for playing around but you should really think about using rails tests to do this kind of stuff. It will save a ton of typing. Here is an example test for this method:

def test_isanumber   a = ApplicationController.new   assert_equal( true, a.isanumber?( 5 ))   assert_equal( false, a.isanumber?( "foo" )) end

Then you just run the test instead of tying it into the console.

Aaron

I have that in app/controllers/application.rb except for

   a) "regex" isn't uppercase        I understand from my Thomas/Hunt Ruy book that all uppercase names        without a @ $ or @@ are constants.    b) I was using "@regex"       I was hoping to do things like define

            upperalpahnumeric as the union of                    #{@regex['upperalpha']} and #{@regex['numeric']}

       or some such.

And why in "environment.rb" ??

Anyway:

If in the console I define @regex then @regex['numeric'].match("123") does work. But only if I define it in the console.

So I think the problem has to do with scope - somehow I'm not referring to what I defined in app/controllers/application.rb

If putting it in environment.rb gives it 'more scope', then my problem becomes

       How do I refer to things - including methods such as 'validnumber()'        that I define or declare in application.rb ?

Adam Groves said the following on 02/21/2007 04:49 PM:

Anton Aylward wrote:

   @regex = {           'upperalpha' => /[A-Z]/,           'loweralpha' => /[a-z]/,           'numeric' => /[0-9]/           'upperalphanumeric' => /[A-Z0-9]/,           'loweralphanumeric' => /[a-z0-9]/,           'alphanumeric' => /[A-Za-z0-9]/    }

Unrelated to your question but if you want readabilty in your regexps then using posix character classes would serve the same intent. For example your @regex['numeric'] would become /[[:digit:]]/

Now methods defined in application.rb (ApplicationController) are only available inside other controllers. If you want broader scoping you could add to Object, or even better to new module/class or existing one depending on your aesthetics.

Zsombor

Unrelated to your question but if you want readabilty in your regexps then   using posix character classes would serve the same intent. For example your @regex['numeric'] would become /[[:digit:]]/

Ah. Good. Thank you.

Where can I find details of that for Ruby? The on-line version of Thomas/Hunt's "Programming Ruby" doesn't mention the possible character classes.

I also wonder about building, for example

         @regex['alphanumeric'] from          @regex['numeric'], @regex['loweralpha'] and @regex['upperalpha']

Or the POSIX equivalent

Now methods defined in application.rb (ApplicationController) are only available inside other controllers. If you want broader scoping you could add to Object, or even better to new module/class or existing one depending on your aesthetics.

Ah. That explain why I can't see them in console. I;d have to do...

a = ApplicationController.new

Or the controller or class they are defined in ...

a.regex['numeric'].match( possible_number.to_s )

Hmm. If I'm going to put them in a new class, then I can break down where they are defined and make them only locally relevant. But that gets back to being able to build regexs out of smaller regexes.