Testing ... with console


#1

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.


“Opportunity is missed by most people because it is dressed in overalls
and
looks like work.”
– Thomas A. Edison


#2

Hi Anton,

I’m by no means an expert but try this:

in environment.rb

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


#3

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 G. said the following on 02/21/2007 04:49 PM:

      'upperalphanumeric' => /[A-Z0-9]/,
      'loweralphanumeric' => /[a-z0-9]/,
      'alphanumeric' => /[A-Za-z0-9]/

}


If God does not write LisP, God writes some code so similar to
LisP as to make no difference.


#4

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.


There is no use whatever trying to help people who do not help
themselves.
You cannot push anyone up a ladder unless he be willing to climb
himself.
- Andrew Carnegie


#5

Anton A. 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


Company - http://primalgrasp.com
Thoughts - http://deezsombor.blogspot.com