Help with relative address

I am using the wiki tutorial to try to learn wxruby. I have programmed
the
windows interface many times. The first two samples ran fine, but now
I am
on the icon frame and need a little help. I am aware that I could
replace
the offending path with a hard coded one, but do not want to do that.

I have 50 years programming experience but no experience with ruby or
other
modern scripting languages.

The code found near the bottom of

http://wxruby.rubyforge.org/wiki/wiki.pl?Frames_(Part_1)

is copied below.

My current OS is
Windows XP
service pack 3
version 2002
AMD processor
2.50 gig of ram.

When I run the RubyMine debugger I get the following error.
Uncaught exception: Icon file does not exist: ./icons/wxwin.ico

If I search my machine I find the file at

C:\Ruby192\lib\ruby\gems\1.9.1\gems\wxruby-ruby19-2.0.1-x86-mingw32\samples\bigdemo\icons

How do I change the line highlighted in red below so that ruby will
find
this file?

---------------------------------Code

require “rubygems”
require “wx”

ID_ICON = 1000

class IconFrame < Wx::Frame
def initialize
super(nil, -1, “Changing Icons!”, Wx::DEFAULT_POSITION,
Wx::Size.new(225,150))
panel = Wx::Panel.new(self, -1)
iconNames = [“wxwin”, “mondrian”, “ogl”, “smiley”]
icons = Wx::RadioBox.new(panel, ID_ICON, “&Icons”,
Wx::Point.new(20,5),
Wx::DEFAULT_SIZE, iconNames, 1,
Wx::RA_SPECIFY_COLS)
evt_radiobox(ID_ICON) {|event| on_change_icon(event)}
if Wx::PLATFORM == “WXMSW”
Wx::Icon.new("./icons/wxwin.ico", Wx::BITMAP_TYPE_ICO)
else
Wx::Icon.new("./icons/wxwin16x16.xpm", Wx::BITMAP_TYPE_XPM)
end

    show(true) #true is the default value, so it may be left off
end

def on_change_icon(event)
    if Wx::RUBY_PLATFORM == "WXMSW"
        case event.get_int   #(get_int is deprecated)
            when 0
                set_icon(Wx::Icon.new("./icons/wxwin.ico"))
            when 1
                set_icon(Wx::Icon.new("./icons/mondrian.ico"))
            when 2
                set_icon(Wx::Icon.new("./icons/ogl.ico"))
            when 3
                set_icon(Wx::Icon.new("./icons/smiley.ico"))
        end
    else
        case event.get_int
            when 0
                set_icon(Wx::Icon.new("./icons/wxwin16x16.xpm"))
            when 1
                set_icon(Wx::Icon.new("./icons/mondrian.xpm"))
            when 2
                set_icon(Wx::Icon.new("./icons/ogl.xpm"))
            when 3
                set_icon(Wx::Icon.new("./icons/smiley.xpm"))
        end
    end
end

end

class MinimalApp < Wx::App

def on_init
    IconFrame.new
end

end

MinimalApp.new.main_loop
----------------------end of
code------------------------------------------------


Ann M.
[email protected]

Hi Ann

On 30/04/11 17:17, Ann M. wrote:

I am using the wiki tutorial to try to learn wxruby. I have
programmed the windows interface many times. The first two samples
ran fine, but now I am on the icon frame and need a little help. I am
aware that I could replace the offending path with a hard coded one,
but do not want to do that.

It’s looking for the icon file relative to the current Ruby working
directory (same as it would if you used standard File.open from Ruby).
You mentioned that you’re running in RedMine and it looks like it’s not
setting the working directory to the one in which the script is saved.

There are a couple of ways round this:

  1. Use Dir.chdir at the start of your script to switch Ruby’s working
    directory.

Dir.chdir(File.dirname(FILE))

But obviously this affects other things

How do I change the line highlighted in red below so that ruby will
find this file?

Wx::Icon.new("./icons/wxwin.ico", Wx::BITMAP_TYPE_ICO)

  1. If you only want to change the one line, you could look up the
    location relative to the script, something like

Wx::Icon.new( File.join( File.dirname(FILE), ‘icons’, ‘wxwin.ico’) )

In general, File.dirname(FILE) is a bit cumbersome, but proposals to
introduce DIR into Ruby core don’t seem to have been concluded:

http://redmine.ruby-lang.org/issues/3346

cheers
alex

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