Re: Thousands of words on Ruby

James,

What do you do for ‘rename method’ in TextMate? I’m aware of the search
and replace in file feature but I was wondering if there was something
better.

Nice write-up by the way. Ruby IDEs might not do all that Eclipse or
NetBeans does for Java, but they’re not as primitive as Tim made them
out to be! I was also surprised that he hadn’t tried Builder.

Steve

On Sep 7, 2006, at 2:11 PM, Molitor, Stephen L wrote:

What do you do for ‘rename method’ in TextMate? I’m aware of the
search
and replace in file feature but I was wondering if there was something
better.

First, let me admit this is the point I have the least ideal solution
for and I am interested in a Ruby Refactoring library I can wrap in
TextMate commands. Remember though, knowing everything about a Ruby
script is all but impossible until runtime. Given that, such a
library would likely function off of heuristics, and that’s about as
accurate as…

I use TextMate’s Find in Project with a hand rolled regular
expression. For your example of a method call I might try something
like:

  Find:  (\.|^[ \t]*)method_name\b

Replace: $1new_name

I can sometimes refine that a little depending on my knowledge of the
project at hand. I always do a Find first, reality-check the
matches, then Replace All. I find this works a very high percentage
of the time, though I do make mistakes, of course.

Here are my solutions to the other points:

  There should be a background parser running all the time so  

that you always know if you have syntax errors and can jump to them
with one click; it is so totally a waste of time for me to save,
then try to run, a file that the computer is in a position to know
won?t work.

I built a TextMate command scoped to Ruby source with a key
equivalent of apple-S that takes the document as input and asks TM to
save the current document when triggered. (This essentially
overrides Save in Ruby files, performs the Save, and allows me to
hook in additional functionality.

I feed the document to ruby -c. If it checks out, I display a
Syntax OK tool tip (default output for this command). If errors are
found, I use the exit_codes.rb support library that ships with
TextMate to switch the output to HTML and display hyperlinked-back-to-
the-source error messages.

This command is an example in my upcoming book:

http://www.pragmaticprogrammer.com/titles/textmate/index.html

  I shouldn?t have to type the names of well-known methods,  

like File.new or (anything).each, or type in closing parentheses or
the keyword end, or fill in more than a couple of characters of
begin/rescue/ensure structures; it is never correct for a human to
hit keys when a computer, in principle, could provide the input.

One word: snippets.

TextMate ships with all the snippets I have written for Ruby and then
some.

  I should never have to scroll much; IDEs go to a lot of  

trouble to make it trivial to jump from wherever to the source for
the method being called, or its docs, or the next compile error or
breakpoint, or variable declaration, or whatever. Scrolling back
and forth in a source-code file is just stupid.

Apple-T to zoom to the needed file, shift-apple-T to zoom to the
needed method. Once you get use to how it matches names you can go
anywhere in an instant:

  1. apple-T
  2. bit-return (takes me to test/functional/beta_invite_test.rb)
  3. shift-apple-T
  4. teir-return (takes me to test_email_is_required)
  Unit testing should be part of the infrastructure. To create  

a test, or run a test, or look at test results, you shouldn?t have
to hit more than one keystroke.

Apple-R to run a test file, or shift-apple-R to run just the current
test. Use zentest to auto-generate the tests (you can wrap that in
a TextMate command with about three lines of Ruby, if you like).

Hope this helps.

James Edward G. II

On Sep 7, 2006, at 11:37 PM, Devin M. wrote:

command TM to save?
The “Save” dropdown menu at the top of the Bundle Editor when you are
editing a command. :wink:

James Edward G. II

http://www.pragmaticprogrammer.com/titles/textmate/index.html
Err, oh. I take it that means it’d be too much to ask how you command TM
to save?

Devin
(what’s the emoticon for “sad puppy face”?)

On Sep 9, 2006, at 9:41 AM, Alex Y. wrote:

Ok, so it’s a real bodge to try to rename a method before runtime.
What happens if we annotate the method to be renamed the next time
it’s run? We could run the script in question under an environment
not totally unlike xmp. The first thing it would do is alter the
source file according to the annotation. It would then add an
appropriate method_missing to Object to do a rename in the original
source file whenever the method was called, and then load() the file.

And what happens if the execution never calls the given method, for
whatever reason, or only reaches some of the places it is used?

James Edward G. II

James Edward G. II wrote:

And what happens if the execution never calls the given method, for
whatever reason, or only reaches some of the places it is used?

That’s why you run your test suite through it. You do have 100% line
coverage, right? :slight_smile:

On Sep 9, 2006, at 10:33 AM, Alex Y. wrote:

And what happens if the execution never calls the given method,
for whatever reason, or only reaches some of the places it is used?
That’s why you run your test suite through it. You do have 100%
line coverage, right? :slight_smile:

Well it’s a clever idea. Show us the prototype. :wink:

James Edward G. II

James Edward G. II wrote:

source file whenever the method was called, and then load() the file.
And what happens if the execution never calls the given method, for
whatever reason, or only reaches some of the places it is used?
That’s why you run your test suite through it. You do have 100% line
coverage, right? :slight_smile:

Well it’s a clever idea. Show us the prototype. :wink:

Well, you asked for it :slight_smile: The usual provisos for prototype code apply:

$ cat rename_method.rb
$obj_replacement_table = {}
class Object
def method_missing(sym, *args, &block)
if $obj_replacement_table.has_key? sym.to_s
filename, line_num = caller[0].split(’:’)[0,2]
l = line_num.to_i - 1
line_arr = File.readlines(filename)
line_arr[l] = line_arr[l].sub(sym.to_s,
$obj_replacement_table[sym.to_s])
File.open(filename, ‘wb’){|f| f.write line_arr}
self.send($obj_replacement_table[sym.to_s].to_sym, *args, &block)
end
end
end

def process_file(filename)
line_arr = File.readlines(filename)
line_arr.each_with_index do |line,i|
if line.chomp =~ /(\s+)def (\w+)(.)#\sbecomes\s+(\w+)/
line_arr[i] = “#{$1}def #{$4}#{$3}# was #{$2}\n”
$obj_replacement_table[$2] = $4
end
end
File.open(filename, ‘wb’){|f| f.write line_arr }
end

process_file(ARGV[0])
load(ARGV[0])

$ cat subject.rb
class TestClass
def my_method(foo) # becomes my_new_method
puts foo
end
end

def meth_calling_renamed(t)
t.my_method(‘Ok2’)
end

t = TestClass.new
t.my_method(‘Ok1’)
meth_calling_renamed(t)

$ ruby rename_method.rb subject.rb
Ok1
Ok2

$ cat subject.rb
class TestClass
def my_new_method(foo) # was my_method
puts foo
end
end

def meth_calling_renamed(t)
t.my_new_method(‘Ok2’)
end

t = TestClass.new
t.my_new_method(‘Ok1’)
meth_calling_renamed(t)

There’s a lot of scope for improvement in there, obviously, but that’s
the general idea.

James Edward G. II wrote:

On Sep 7, 2006, at 2:11 PM, Molitor, Stephen L wrote:

What do you do for ‘rename method’ in TextMate? I’m aware of the search
and replace in file feature but I was wondering if there was something
better.

First, let me admit this is the point I have the least ideal solution
for and I am interested in a Ruby Refactoring library I can wrap in
TextMate commands. Remember though, knowing everything about a Ruby
script is all but impossible until runtime.

Brainstorm just happened, so bear with me - this is straight off the top
of my head, and may be stupid:

Ok, so it’s a real bodge to try to rename a method before runtime. What
happens if we annotate the method to be renamed the next time it’s run?
We could run the script in question under an environment not totally
unlike xmp. The first thing it would do is alter the source file
according to the annotation. It would then add an appropriate
method_missing to Object to do a rename in the original source file
whenever the method was called, and then load() the file.

Good idea? Worth trying? Hideously flawed?