program to rename files : improvements

Discussion in 'Ruby' started by Giovanni Alverà, Feb 14, 2011.

  1. 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?

    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
    (e.g. a loop)

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

    Giovanni Alverà, Feb 14, 2011
    1. Advertisements

  2. Giovanni Alverà

    Joel F. Guest

    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:

    Let's consider this code segment from your app:

    @chkBtnReplace1 ="Replace")
    layout.put(@chkBtnReplace1, 20, 80)
    @chkBtnReplace1.signal_connect("clicked") do |w|
    @chkBtnReplace2 ="Replace")
    layout.put(@chkBtnReplace2, 20, 120)
    @chkBtnReplace2.signal_connect("clicked") do |w|

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

    (1..4).each do |i|
    buttonName = "@chkBtnReplace" + i

    button = self.instance_variable_get(buttonName)

    layout.put(button, 20, 40 + i*40)
    button.signal_connect("clicked" do |w|

    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,"Replace"))

    Joel F., Feb 15, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.