From: "Ara.T.Howard" Date: 2004-04-14T07:54:14+09:00 Subject: Re: New Ruby questions... On Tue, 13 Apr 2004, Jeff Massung 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. So, onto a couple questions that I can't > seem to find the answers to: welcome aboard. > Quickly, the "teach myself" project is a simple ARM assembler. 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). you need to make a pattern that says "a register string is an 'r' followed by one, or more, digits". pattern matching is greedy by default, so that alone will do it irb(main):001:0> register_pat = /r\d+/o => /r\d+/ irb(main):002:0> "r1"[register_pat] => "r1" irb(main):003:0> "r11"[register_pat] => "r11" irb(main):004:0> "r111"[register_pat] => "r111" however, pattern matching is trickier than it looks: irb(main):005:0> "car_number1"[register_pat] => "r1" you probably wan't to say something like: "a register is the string 'r' followed by one, or more, digits, but when the whole thing is bounded by non-word chars" irb(main):007:0> "r1"[pat] => "r1" irb(main):008:0> "carr1"[pat] => nil in general, it's best to always anchor patterns somehow. > 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? > > I noticed that if I have a global variable: > > x = 10 > > In some function: > > def show_x print x end > > This fails (unknown local x). But if I use @x in both cases it works just > fine. Can someone explain this to me? Also, what about @@ variables? > Likewise, is there @@@ or @@@@ ? '@' means instance var - it's OO speak for __my__ var. eg: ~ > cat foo.rb class Car attr :model def initialize model @model = model end end audi = Car.new 'audi' p audi.model jag = Car.new 'jaguar' p jag.model ~ > ruby foo.rb "audi" "jaguar" '@@' means class var. they could be thought of as 'static storage on a class hierachy basis'. eg: ~ > cat foo.rb class Widget @@total_widgets = 0 def Widget.n_widgets @@total_widgets end attr :name def initialize name @name = name @@total_widgets += 1 end end widgets = [] 42.times{|i| widgets << Widget.new(i) } p widgets.size p Widget.n_widgets ~ > ruby foo.rb 42 42 there are also, 'class instance vars', but that's a longer story... there is no @@@+ > Thanks in advance. no problem. you'll find this (the pickaxe) most useful: http://www.pragmaticprogrammer.com/ruby/downloads/book.html i use it, literally, every day. i always use the electronic copy, but bought three and gave them away to support the authors - good to support them too, i hear they are working on a new release... cheers. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | URL :: http://www.ngdc.noaa.gov/stp/ | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done ===============================================================================