Regular Expresison in Method Problem

Hi,

Why does the following give me a problem:

def calc(inValue)

if(inValue =~ /^([-+]?[0-9]+)([CF])$/)
InputValue = $1
InputType = $2
end
end

calc(‘22C’)

Error reported is:

RegExp.rbw:24: dynamic constant assignment
InputType = $2

Thanks,

Nick.

Hi –

···

On Thu, 27 Mar 2003, Nick wrote:

Hi,

Why does the following give me a problem:

def calc(inValue)

if(inValue =~ /^([-+]?[0-9]+)([CF])$/)
InputValue = $1
InputType = $2
end
end

calc(‘22C’)

Error reported is:

RegExp.rbw:24: dynamic constant assignment
InputType = $2

It gives that error message because you’ve dynamically assigned to a
constant :slight_smile: Names beginning with uppercase letters are constants.
Try input_value and input_type.

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Because in Ruby, constants start with an uppercase letter.
Use a lowercase letter as start of the variable name instead.

-Martin

···

On Thu, Mar 27, 2003 at 07:10:04AM +0900, Nick wrote:

Hi,

Why does the following give me a problem:

def calc(inValue)

if(inValue =~ /^([-+]?[0-9]+)([CF])$/)
InputValue = $1
InputType = $2
end
end

calc(‘22C’)

Error reported is:

RegExp.rbw:24: dynamic constant assignment
InputType = $2

ah…doh…thanks for that!

dblack@superlink.net wrote in message
news:Pine.LNX.4.44.0303261720580.7205-100000@candle.superlink.net

···

Hi –

On Thu, 27 Mar 2003, Nick wrote:

Hi,

Why does the following give me a problem:

def calc(inValue)

if(inValue =~ /^([-+]?[0-9]+)([CF])$/)
InputValue = $1
InputType = $2
end
end

calc(‘22C’)

Error reported is:

RegExp.rbw:24: dynamic constant assignment
InputType = $2

It gives that error message because you’ve dynamically assigned to a
constant :slight_smile: Names beginning with uppercase letters are constants.
Try input_value and input_type.

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav