1

I'm using Curses to create a command line GUI with Ruby. Everything's going well, but I have hit a slight snag. I don't think Curses knowledge (esoteric to be fair) is required to answer this question, just Ruby concepts such as objects and inheritance.

I'm going to explain my problem now, but if I'm banging on, just look at the example below.

Basically, every Window instance needs to have .close called on it in order to close it. Some Window instances have other Windows associated with it. When closing a Window instance, I want to be able to close all of the other Window instances associated with it at the same time.

Because associated Windows are generated in a logical fashion, (I append the name with a number: instance_variable_set(self + integer, Window.new(10,10,10,10)) ), it's easy to target generated windows, because methods can anticipate what assosiated windows will be called, (I can recreate the instance variable name from scratch, and almost query it: instance_variable_get(self + integer).

I have a delete method that handles this. If the delete method is just a normal, global method (called like this: delete_window(@win543) then everything works perfectly. However, if the delete method is an instance method, which it needs to be in-order to use the self keyword, it doesn't work for a very clear reason; it can 'query' the correct instance variable perfectly well (instance_variable_get(self + integer)), however, because it's an instance method, the global instances aren't scoped to it!

Now, one way around this would obviously be to simply make a global method like this: delete_window(@win543). But I have attributes associated with my window instances, and it all works very elegantly.

This is very simplified, but it literally translates the problem exactly:

class Dog
        def speak
                woof
        end
end

def woof
        if @dog_generic == nil
                puts "@dog_generic isn't scoped when .woof is called from a class method!\n"
        else
                puts "@dog_generic is scoped when .woof is called from the global scope. See:\n" + @dog_generic
        end
end

@dog_generic = "Woof!"
lassie = Dog.new
lassie.speak #=> @dog_generic isn't scoped when .woof is called from an instance method!\n

woof #=> @dog_generic is scoped when .woof is called from the global scope. See:\nWoof!

TL/DR: I need lassie.speak to return this string: "@dog_generic is scoped when .woof is called from the global scope. See:\nWoof!" @dog_generic must remain as an insance variable. The use of Globals or Constants is not acceptable.

Could woof inherit from the Global scope? Maybe some sort of keyword:

def woof < global # This 'code' is just to conceptualise what I want to do, don't take offence!
end

Is there some way the .woof method could 'pull in' @dog_generic from the global scope? Will @dog_generic have to be passed in as a parameter?

1 Answer 1

1

You should be able to get that from instance_variable_get(:@dog_generic) but there is an implied self in that call. So, this means we would have to specify exactly what object you want to get that instance variable from. And the fact that it is an instance variable implies you need to specify which instance of which object. Therein lies the puzzle, right?

And so, somewhere, you could pass self from that point, where self is 'main' (which is of class Object).

class Dog
  def initialize(generic_dog)
    @from_outside = generic_dog # => main
  end
  def speak
    @from_outside.instance_variable_get(:@dog_generic) # => "Woof!"
  end
end

def woof
  if @dog_generic == nil
    "@dog_generic isn't scoped when .woof is called from a class method!\n"
  else
    "@dog_generic is scoped when .woof is called from the global scope. See:\n" + @dog_generic
  end
end

@dog_generic = "Woof!"
lassie = Dog.new(self)
puts lassie.speak
puts woof

# Output shown as >> and returns shown as =>
# >> Woof!
# >> @dog_generic is scoped when .woof is called from the global scope. See:
# >> Woof!

So, no, @dog_generic doesn't necessarily need to be passed in as an argument, but you do need to pass something in somewhere.

I changed the if statement to not use puts as unconditionally putsing is not DRY, and we probably want to return the result of the_if statement_ rather than return nil from the method. So we puts the method being called.

2
  • Excellent answer, works perfectly. Could you explain more about the :@dog_generic though? The colon? Never seen anything like that.
    – Crazy JIm
    Commented Oct 25, 2013 at 2:09
  • That is just simply a symbol. Just like :name or :woof are symbols. @from_outside.instance_variables will give you that symbol, just like methods method will give you an array of methods and they are all listed as symbols.
    – vgoff
    Commented Oct 25, 2013 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.