UberGM
UberGM

Reputation: 99

Write contents of textbox to file in Shoes.rb

I've been working on a text editor in shoes and ruby, but have run into a problem. I can open files perfectly fine, but when I try to save a file, I can't write the contents of the textbox to the file.

Shoes.app :title => "Reditr", :width => 640, :height => 430 do
  @box = edit_box :width => 1.0, :height => 400, :text =>'Welcome to Reditr!'

  button "Save", :width => 85 do
    file = ask_save_file
    File.open(file, "w+") do |f|
      @file.text = File.write(@box.text)
    end
  end

  button "Open", :width => 75 do
    @file = ask_open_file
    @box.text = File.read(@file)
    @filename.text = @file
  end
end

Upvotes: 2

Views: 539

Answers (1)

Charles Caldwell
Charles Caldwell

Reputation: 17189

I'm rusty when it comes to using Shoes but your File.open seems a little off. When you open a file using .open, the file is passed into the code block. So, in this case, f is your file you are wanting to write to. You are probably looking for something like:

File.open(file, "w+") do |f|
  @file.text = f.write(@box.text)
end

Upvotes: 2

Related Questions