References
Category:DVD companies of Taiwan
Category:Manufacturing companies based in Kaohsiung
Category:Entertainment companies established in 2006
Category:Privately held companies of Taiwan
Category:Taiwanese brandsQ:
Why does this ruby instance variable behave like an instance variable?
I have an instance variable in a ruby file that I can access within the same class. For example,
class Foo
def run
# This returns bar
self.bar
end
def bar
puts "The foo is #{@foo.inspect}"
@foo = "blah"
end
def foo=(value)
@foo = value
end
def foo
return @foo
end
end
But, if I define a global variable, say, bar, and do
# outside of the class
@bar = "blah"
and then try to access the instance variable through the class, it doesn't work.
Foo.new.foo # => undefined method `foo' for #
I'm fairly new to ruby and I don't understand why that happens. Can someone shed some light on this?
A:
Ruby is a lexically scoped language, as is Java, so your @bar = "blah" is outside of run and, therefore, @bar is not defined in the scope of the Foo class.
The same is true of instance variables.
There are many ways to fix your issue, all of which boil down to the same solution: move the code that modifies @foo inside the run method.
Providers of digital video content, such as cable television, satellite television, broadband Internet and other sources of video, may provide video-on-demand (VOD) services to subscribers. A VOD service enables a viewer to view video content on the viewer's choice of viewing device, and at the viewer's convenience. In addition, a VOD service may enable the viewer to set up and control the viewing of video content, such as movies or television programs, through the use of digital video recorders (DVRs), personal video record ac619d1d87
Related links:
Comments