# Help with relative address

**URL:** <https://www.ruby-forum.com/t/help-with-relative-address/206420>\
**Category:** wxRuby\
**Created:** [April 30, 2011, 7:00pm UTC](https://www.ruby-forum.com/t/help-with-relative-address/206420 "2011-04-30T19:00:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 30, 2011, 7:00pm UTC](https://www.ruby-forum.com/t/help-with-relative-address/206420/1 "2011-04-30T19:00:06Z")

</div>

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)](http://wxruby.rubyforge.org/wiki/wiki.pl?Frames_%28Part_1%29)

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.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ann\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@ann\_m](https://www.ruby-forum.com/u/ann_m)\
**Post date:** [May 1, 2011, 12:12pm UTC](https://www.ruby-forum.com/t/help-with-relative-address/206420/2 "2011-05-01T12:12:51Z")

</div>

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:

> **[Feature #3346: \_\_DIR\_\_ revisted - Ruby - Ruby Issue Tracking System](https://redmine.ruby-lang.org/issues/3346)**
>
> Redmine

cheers  
alex
