Request for comments (longish)

Hi all,

I’ve got some relatively hairy code on which I would really appreciate
some comments. It’s relatively succinct, but I’m sure there are a lot of
things I’m missing because of my newness to ruby. The comments should
make it as self-explanatory as possible, but like I said, the code is a
bit hairy.

Any comments, criticism, or approbation are definitely appreciated.

Oh, and thanks Matz! Ruby is definitely awesome; I wish I’d picked it up
much earlier.

Luke Kanies

(btw, for those who remember my lex/yacc thread, I’m going to use racc,
but I realized I’m a ways from needing a parser. Rlex is quite out of
date, but can be hacked to work)

autocheck2.rb (4.84 KB)

···


A government big enough to give you everything you want is big enough
to take from you everything you have. --Gerald R. Ford

Hi –

class Operation
@derivatives = {}
attr_reader :retrieve, :object
attr_writer :retrieve, :fix, :object

operations will be retrieved from the base class by name

should i be using symbols here instead of strings?

I don’t think it matters, except that it might be a little faster with
symbols if you’re doing lots of hash access. If you just want to give
people the choice, you can canonicalize the object with #to_s or
#intern.

def Operation.
unless @derivatives.include?(name)
raise “No class Operation::#{name}”
end

  return @derivatives[name]

end

This is a place where you could, conceivably, use my favorite
1.8.0-and-greater “trick”:

class Operation
@derivatives = Hash.new {|k,v| raise “No class Operation::#{v}” }
# …
def Operation.
@derivatives[name]
end

:slight_smile:

David

···

On Wed, 10 Dec 2003, Luke A. Kanies wrote:


David A. Black
dblack@wobblini.net

Whoops, |h,k| would make more sense as it’s the Hash object itself
plus your key that you get, but you get the idea :slight_smile:

David

···

On Wed, 10 Dec 2003, David A. Black wrote:

@derivatives = Hash.new {|k,v| raise "No class Operation::#{v}" }


David A. Black
dblack@wobblini.net

Love that trick. Here’s how I initialized a hash recently. (The
algorithm called for setting some initial values and then removing
things as necessary.)

players == %w{Sam Min …}

result = Hash.new { |h,k| h[k] = [:white, :black] }
result.values_at(*players)

End result of that:

result == {
“Sam” => [:white, :black],
“Min” => [:white, :black],

}

Oooooh… delicious.

Gavin

···

On Wednesday, December 10, 2003, 10:33:44 PM, David wrote:

This is a place where you could, conceivably, use my favorite
1.8.0-and-greater “trick”:

class Operation
@derivatives = Hash.new {|k,v| raise “No class Operation::#{v}” }
# …
def Operation.
@derivatives[name]
end

Hi –

Thanks for your comments.

# operations will be retrieved from the base class by name
# should i be using symbols here instead of strings?

I don’t think it matters, except that it might be a little faster with
symbols if you’re doing lots of hash access. If you just want to give
people the choice, you can canonicalize the object with #to_s or
#intern.

Yeah, that’s what I’ll most likely do, I just wanted to verify that this
was a common practice before I set it all up.

def Operation.[](name)
  unless @derivatives.include?(name)
  	raise "No class Operation::#{name}"
  end

  return @derivatives[name]
end

This is a place where you could, conceivably, use my favorite
1.8.0-and-greater “trick”:

class Operation
@derivatives = Hash.new {|k,v| raise “No class Operation::#{v}” }
# …
def Operation.
@derivatives[name]
end

Um, wow. I’m awestruck, I think.

Thanks! Ruby is very, very cool.

Luke

···

On Wed, 10 Dec 2003, David A. Black wrote:

On Wed, 10 Dec 2003, Luke A. Kanies wrote:


Due to circumstances beyond your control, you are master of your fate
and captain of your soul.