Inherit one instance variable from the global scope

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?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Inherit one instance variable from the global scope

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?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật