In article <107opa3ca462a41 / corp.supernews.com>,
Jeff Massung  <jma / NOSPAM.mfire.com> wrote:
>I've just started Ruby a couple days ago (man this is cool). Coming from 
>the embedded world of Forth and C, being able to do some string parsing 
>easily is what I'm looking forward to. 

Welcome.  So how did you find out about Ruby?

>So, onto a couple questions that I 
>can't seem to find the answers to:
>
>Quickly, the "teach myself" project is a simple ARM assembler. 

Sounds like a good project.

> I've done 
>it numerous times, and it is something I'm familiar and comfortable with 
>(let alone something I need ATM).
>
>1. How can a regexp get the longer of two possibilities that are 
>ambiguous? For example, I need to be able to strip out register names:
>
>registers = '(r0|r1|r2|r3...|r10|r11|r12...)'
>
>In the regular expression, how can I get it to find r10 or r11 instead of 
>stopping at r1? I get the same problem with opcode mnemonics (like B 
>instead of BX or BL).
>

I'll let someone who is quicker with regexen answer that one.

>2. I don't seem to understand the @ convensions for variable names. Is 
>this just a naming convension that people stick to (like *name* system 
>names in Lisp) but aren't required?

Well, no, it's not just convention.  the '@' implies instance scope.

class SomeClass
  def initialize
    @foo = "some value"
  end

  def to_s
    @foo.to_s
  end
end

The @foo is accessible to the to_s method.  foo is an instance variable.


>
>I noticed that if I have a global variable:
>
>x = 10
>

No, you've got a regular variable, if you want a global variable, put a 
'$' in front, like so:

 $x = 10

>In some function:
>
>def show_x
>  print x

   x inside of the method 'show_x' is different from the 'x' at the scope 
above.

>end
>
>This fails (unknown local x). But if I use @x in both cases it works just 
>fine. Can someone explain this to me? 

if you used @x in both cases it would work because @x would be accessable 
from the current instance scope which happens to be an instance of 
'Object' at this point.

>Also, what about @@ variables? 

'@@' variables have class scope.  So in this case:

class Foo
  @@var = "Foo!"
  def var
    @@var
  end

  def var=(val)
    @@var=val
  end
end

a = Foo.new
b = Foo.new
puts a.var #=> Foo!
puts b.var #=> Foo!
#they're the same
a.var="Bar!"
puts b.var #=> Bar!
#still the same

a and b are instances of class Foo and @@var is the same in both a and b 
because @@var has class scope (all instances of Foo share @@var).

>Likewise, is there @@@ or @@@@ ?

Luckily, we don't have these :)

Phil