On Tue, 26 Feb 2002 07:03:29 GMT, shreeve / s2s.org (Steve Shreeve) wrote:

>b.rpc('ORWU TOOLMENU',[nil])
>b.rpc('ORWU HASKEY',['XUPROGMODE'])
>b.rpc('ORQQPX NEW REMINDERS ACTIVE')
>['O RX', 'I RX', 'UD RX', 'IV RX'].each { |val| b.rpc("ORWORDG IEN", [val]) }
>b.rpc('ORWU DT', ['NOW'])
>b.rpc('ORWCH LOADAL')
>
>Notice how the third to last line is sort of "doing it's own thing". It would 
>be nice have all these lines "look the same". Something like this would work:

For another angle on this question, what we have here is a "code smell", where
not all the code is at the same level of abstraction. It's generally considered
to be a good idea to fix that. 

When I have a bunch of nearly-duplicate code like the above, I try to remove the
duplication.Here the duplication isn't  just "b.rpc", it's that plus "paren, a
string, maybe comma another string, paren". So I think of setting up the
parameters as a data table and looping over the whole thing ...

One approach might be something like this. (I've put in 'puts' and #{} to see
what's up. The real code would be similar but not identical

commands = <<HERE
ORWU TOOLMENU,nil
ORWU HASKEY,XUPROGMODE
ORQQPX NEW REMINDERS ACTIVE
ORWORDG IEN,O RX,I RX,UD RX,IV RX
ORWU DT,NOW
ORWCH LOADAL
HERE

def command(commands)
  commands.each do | line |
    items = line.chomp.split(',').reverse
    command = items.pop
    if items.size == 0
      puts "b.rpc('#{command}')"
    else
      while (arg = items.pop) != nil
        puts "b.rpc('#{command}', ['#{arg}'])"
      end
    end
  end  
end  

command(commands)

The output from the above looks like this:

b.rpc('ORWU TOOLMENU', ['nil'])
b.rpc('ORWU HASKEY', ['XUPROGMODE'])
b.rpc('ORQQPX NEW REMINDERS ACTIVE')
b.rpc('ORWORDG IEN', ['O RX'])
b.rpc('ORWORDG IEN', ['I RX'])
b.rpc('ORWORDG IEN', ['UD RX'])
b.rpc('ORWORDG IEN', ['IV RX'])
b.rpc('ORWU DT', ['NOW'])
b.rpc('ORWCH LOADAL')

Just a thought ...

Ronald E Jeffries
http://www.XProgramming.com
http://www.objectmentor.com
I'm giving the best advice I have. You get to decide whether it's true for you.