Here's a simple snippet of code of a ruby scope pitfall (bug?) that I
keep stumbling upon.
Here a "global" function is invoked in two different contexts and
returns different behavior due to self being set differently.

---
#!/usr/bin/env ruby

@x = 'hello global'

def global_func
  puts "global_func: class:#{self.class} x=#@x"
end

class A
  def test_scope
    global_func
  end
end

global_func

a = A.new
a.test_scope
---

I'm wondering if this is intended behavior in the language.  If
intended, what's the best approach to avoid running into this?  I'm
currently porting code from other languages with different (more
correct to me) scoping, and this keeps tripping me.