ruby - How to program faster, (generate code from a pattern?) -


i run problems solved automating code writing, aren't long enough justify tediously entering each piece faster.

here example:

putting lists dictionaries , things this. converting b.

a

hotdog          hd hamburger       hb hat             h 

b

def symbolizetype     case self.type     when "hotdog"         return "hd"     when "hamburger"         return "hb"     when "hat"         return "h" end 

sure come automatically, make sense if list 100+ items long. list of 10-20 items, there better solution tediously typing? ruby example, typically run cases time. instead of case statement, maybe it's dictionary, maybe it's list, etc.

my current solution python template streaming input , output in place, , have write parsing , output code. pretty good, there better? feel vim macro excel at, i'm experienced vim. can vim easily?

i frequently, , use single register , macro, i'll share.

simply pick register, record keystrokes, , replay keystrokes register.

this long explanation, process extremely simple , intuitive.

here steps take:

a. starting text

hotdog          hd hamburger       hb hat             h 

b. insert initial, non-repetitive lines preceding text transform

def symbolizetype     case self.type hotdog          hd hamburger       hb hat             h 

c. transform first line, while recording keystrokes in macro

this step i'll write out in detailed sub-steps.

  1. place cursor on first line transform ("hotdog") , type qa begin recording keystrokes macro register a.
  2. type ^ move cursor start of line
  3. type transform line want, me comes out looking following macro

    ^i^iwhen "^[ea"^[ldwi^m^ireturn "^[ea"^[j

    where ^i tab, ^[ esc, , ^m enter.

  4. after line transformed liking, move cursor next line want transform. can see in macro above final j @ end.

    this allow automatically repeat macro while cycles through each repetitive line.

  5. stop recording macro typing q again.

  6. you can replay macro register a many times using standard vim count prefix, in case 2 consecutive times starting next line transform.

    2@a

this gives following text

def symbolizetype     case self.type     when "hotdog"         return "hd"     when "hamburger"         return "hb"     when "hat"         return "h" 

d. finally, insert ending non-repetitive text

def symbolizetype     case self.type     when "hotdog"         return "hd"     when "hamburger"         return "hb"     when "hat"         return "h" end 

final comments

this works quick random, repetitive text, , find fluent.

simply pick register, record keystrokes, , replay keystrokes register.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -