# Need help opening and saving a file with FileChooser

**URL:** <https://www.ruby-forum.com/t/need-help-opening-and-saving-a-file-with-filechooser/90117>\
**Category:** Ruby-Gnome 2\
**Created:** [March 26, 2007, 6:12am UTC](https://www.ruby-forum.com/t/need-help-opening-and-saving-a-file-with-filechooser/90117 "2007-03-26T06:12:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [March 26, 2007, 6:12am UTC](https://www.ruby-forum.com/t/need-help-opening-and-saving-a-file-with-filechooser/90117/1 "2007-03-26T06:12:23Z")

</div>

I have a question about using ruby and ruby-gtk2. I have a simple gui  
and am using SourceView as my text buffer and have added a menu to open  
and save files. My question is how in the world to I open a file and  
display it within my buffer?

so far my open method is:

callback\_open = Proc.new {  
p “Open is called.”  
dialog = FileChooserDialog.new(“Open File”,nil,  
FileChooser::ACTION\_OPEN,nil,  
[Stock::CANCEL,Dialog::RESPONSE\_CANCEL],  
[Stock::OPEN,Dialog::RESPONSE\_ACCEPT])  
if dialog.run == Dialog::RESPONSE\_ACCEPT  
@filename = #{dialog.filename}  
file = File.open(@filename)

}

after the file variable line I have tried different things to actually  
add the text to the buffer but none seem to work. I checked the API but  
I’m not sure if their is a TextBuffer method that I can use to read in  
the file line by line or if I just call File.open and pass the filename  
and rad it line by line. When  
I try to do that it outputs the text in the console not in my buffer.

Any help would be appreciated.

David T.

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [March 26, 2007, 6:47am UTC](https://www.ruby-forum.com/t/need-help-opening-and-saving-a-file-with-filechooser/90117/2 "2007-03-26T06:47:54Z")

</div>

I believe something along a call to the following open\_file will do the  
trick.

def read\_file  
File.open(@filename){|f| ret = f.readlines.join }  
end

def open\_file  
text = read\_file  
@buffer.text= text  
end

view = Gtk::TextView.new  
@buffer = view.buffer

There is a simple texteditor on the home page (section samples). It may  
help.

Alexis

On 3/26/07, David T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> FileChooser::ACTION_OPEN,nil,
> 
> ```
> 
> I’m not sure if their is a TextBuffer method that I can use to read in
> 
> * * *
> 
> Take Surveys. Earn Cash. Influence the Future of IT  
> Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share your  
> opinions on IT & business topics through brief surveys-and earn cash  
> [http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> removed\_email\_address@domain.invalid  
> [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys-and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)
