Program to rename files : improvements

Hello,

I wrote a program to rename files in a user-defined
directory. The program lets you choose the directory,
type in some “Entries” the strings to replace and the
related strings to replace with.
Then when I click the “Start” button the program perform
the replacements.

This is my first program in Ruby and I am trying to improve
it (e.g make it faster & well coded) do you have any suggestions?

Especially

  1. the “Select Directory” FileChooserDialog does not quit when I click
    on the “Cancel” Button and it quits only the 2nd time I ckick the “X”
    button on the top of the window.

  2. is there a way to fill up faster the hash with the strings to
    replace?
    (e.g. a loop)

I hope you understand my english :slight_smile:
Thank you, Giovanni

What up Gio,

In your post subject you should mention that you’re using Gtk so that
someone with Gtk experience will be likely to respond. I’ve never used
it so I can’t help you with your problem (1)

However, as for changing your code…

If you follow this link the first method you see might be helpful:
http://www.ruby-doc.org/core/classes/Object.html#M001029

Let’s consider this code segment from your app:

@chkBtnReplace1 = CheckButton.new(“Replace”)
layout.put(@chkBtnReplace1, 20, 80)
@chkBtnReplace1.signal_connect(“clicked”) do |w|
on_clicked(w)
end
@chkBtnReplace2 = CheckButton.new(“Replace”)
layout.put(@chkBtnReplace2, 20, 120)
@chkBtnReplace2.signal_connect(“clicked”) do |w|
on_clicked(w)
end

If I wanted to get bonus crazy ruby points, I might do something like
this instead:

(1…4).each do |i|
buttonName = “@chkBtnReplace” + i

self.instance_variable_set(buttonName, CheckButton.new(“Replace”))
button = self.instance_variable_get(buttonName)

layout.put(button, 20, 40 + i*40)
button.signal_connect(“clicked” do |w|
on_clicked(w)
end
end

The Object class in Ruby is pretty cool and is worth reading through.
I’m not very experienced but it looks like “instance_variable_set”
returns the object that it sets, so you might be able to shorten the
instance variable stuff to:

button = self.instance_variable_set(buttonName,
CheckButton.new(“Replace”))

<3
joel

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs