Update value displayed based on instance variable?

204 Views Asked by At

As far as I know, if all I wanted to do was do "puts" in a console, then I would not be having to ask this question. (However, finally I am asking you all at StackOverflow myself, though I've been visiting for years.)

Here is my issue:

  • I am trying to create a variable which will be "set" to a specific value upon user click
  • I am then trying to display that value after it is changed
  • I can set the value, but it does not get displayed

(Of course, this should work if I am not using Shoes.)

Here is the relevant portion of my code:

class Inits
# Declares all global vars used
  def init1
    ...
  end
  def initattrib
    @id = "###"
    @name = "name undef"
    alert("Alert: default attrib set")
  end

  def id
    @id
    #alert("Alert: id call")
  end

  def name
    @name
    #alert("Alert: name call")
  end

  def id=(newid)
    @id = newid
    #alert("Alert: id set")
  end

  def name=(newname)
    @name = newname
    #alert("Alert: name set")
  end
end

Then I am trying to call the id and set it as so:

Shoes.app :width => 800, :height => 600, :resizable => false do
  currclass = Inits.new

  currclass.init1
  currclass.initattrib

  .
  .
  .

  id = "123"
  name = "new name"

  # I declare something to click here
  click { currclass.id = id, currclass.name = name }

  # Then I try to display it as so:
  para currclass.id
  para currclass.name

  # But of course the value is not displayed -- just the default value
end

... As an aside, I am pretty sure I should be using instance variables and not class variables (@x, not @@x).

Is there some way I can "update on change" ("clock rising edge" is a good analogy) or some other way to call this?

Anyhow, thank you in advance for any advice on what I am not doing correctly. Perhaps there is a misunderstanding.

2

There are 2 best solutions below

0
On

If I understood correctly what you want to do you should do it like that:

class Cos
attr_accessor :co
def initialize(cos)
@co=cos
end
end
Shoes.app :width => 800, :height => 600, :resizable => false do
 @cosik = Cos.new("ddd")
 @ap=para(@cosik.co)

 button "click" do
  @cosik.co = "oj"
  @[email protected]
 end

end

K

0
On

The first thing you learn using Shoes: It is a metaprogramming jungle.

Shoes has hooks to default procedures, and I'm sure it has one for Class creation as well so Shoes can add its own wizardry to that Class being specified. Which would mean that a Class - defined outside of Shoes.app - might now work the same as if defined inside Shoes.app.

Try moving everything inside Shoes.app block, I had a lot of problems with pieces of code laying outside of Shoes.app (usually scope problems).