# Functions scope

**URL:** <https://www.ruby-forum.com/t/functions-scope/89788>\
**Category:** Ruby\
**Created:** [March 29, 2006, 8:55am UTC](https://www.ruby-forum.com/t/functions-scope/89788 "2006-03-29T08:55:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mirek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@Mirek](https://www.ruby-forum.com/u/Mirek)\
**Post date:** [March 29, 2006, 8:55am UTC](https://www.ruby-forum.com/t/functions-scope/89788/1 "2006-03-29T08:55:08Z")

</div>

class C  
def open()  
# how to call global open function from here?  
end  
end

---

<div class="post-metadata">

**Author:** ![Mirek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@Mirek](https://www.ruby-forum.com/u/Mirek)\
**Post date:** [March 29, 2006, 9:00am UTC](https://www.ruby-forum.com/t/functions-scope/89788/2 "2006-03-29T09:00:10Z")

</div>

i think you can do

::open

;D

On 29/03/06, Mirek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Daniel B.  
[http://danielbaird.com](http://danielbaird.com) (TiddlyW;nks! :: Whiteboard Koala :: Blog ::  
Things  
That Suck)  
[[My webhost uptime is ~ 92%… if no answer pls call again later!]]

---

<div class="post-metadata">

**Author:** ![Mirek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@Mirek](https://www.ruby-forum.com/u/Mirek)\
**Post date:** [March 29, 2006, 9:36am UTC](https://www.ruby-forum.com/t/functions-scope/89788/3 "2006-03-29T09:36:19Z")

</div>

> i think you can do  
> ::open

require ‘open-uri’

class C  
def open(uri)  
::open(uri)  
end  
end

o = C.new  
o.open(‘x’)

* * *

open.rb:6: syntax error, unexpected tIDENTIFIER, expecting tCONSTANT  
::open(uri)  
^

---

<div class="post-metadata">

**Author:** ![Mirek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@Mirek](https://www.ruby-forum.com/u/Mirek)\
**Post date:** [March 29, 2006, 10:13am UTC](https://www.ruby-forum.com/t/functions-scope/89788/4 "2006-03-29T10:13:40Z")

</div>

Mirek wrote:

> end
> 
> o = C.new  
> o.open(‘x’)
> 
> * * *
> 
> open.rb:6: syntax error, unexpected tIDENTIFIER, expecting tCONSTANT  
> ::open(uri)  
> ^

:: is use to find a toplevel constant, It is not used for method  
lookups. ie:

A = true  
class B; end  
module C; end

If you are deeply nested you can work your way out by using ::

A = true  
class B  
class A # inner class  
end

```
 def open
   puts "A is #{A}" #puts A::B
 end

 def open2
    puts "A is #{::A}" #puts true, because our toplevel constant A 

```

is true  
end  
end

It is more useful when you are inside of a class or module namespace,  
and you have an inner class  
with a clashing or similar name. E.g: Socket or String, and instead of  
using that one, you want to  
use the toplevel namespace to find what you are looking for.

The code “require ‘open-uri’” actually adds the _private_ method ‘open’  
to to the Kernel module. The  
Kernel module is included (or as folks call it, mixed in) in Object.  
Every class inherits from  
Object, so every class gets the _new_ private open method.

So for your example you could invoke super in your open call to make  
sure it gets passed up the  
chain to Kernel#open.:

require ‘open-uri’

class C  
def open(uri)  
super  
end  
end

o = C.new  
o.open(‘x’)

Hope this helps,

Zach

---

<div class="post-metadata">

**Author:** ![Mirek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@Mirek](https://www.ruby-forum.com/u/Mirek)\
**Post date:** [March 29, 2006, 10:16am UTC](https://www.ruby-forum.com/t/functions-scope/89788/5 "2006-03-29T10:16:56Z")

</div>

Hi,

You can always provide full path to your desired method, like this:

class C  
def open(uri)  
Kernel::open(uri) # open-uri has overriden Kernel::open method  
end  
end

–  
Martins

---

<div class="post-metadata">

**Author:** ![Mirek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@Mirek](https://www.ruby-forum.com/u/Mirek)\
**Post date:** [March 30, 2006, 2:59am UTC](https://www.ruby-forum.com/t/functions-scope/89788/6 "2006-03-30T02:59:04Z")

</div>

Kernel::open, that’s what i was thinking of. Next time i’ll actually  
_test_ stuff before I post an answer for someone ☹

;Daniel

On 29/03/06, Marcin MielÅ¼yÅ?ski [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> lopex

–  
Daniel B.  
[http://danielbaird.com](http://danielbaird.com) (TiddlyW;nks! :: Whiteboard Koala :: Blog ::  
Things  
That Suck)  
[[My webhost uptime is ~ 92%… if no answer pls call again later!]]

---

<div class="post-metadata">

**Author:** ![Mirek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@Mirek](https://www.ruby-forum.com/u/Mirek)\
**Post date:** [March 30, 2006, 4:17am UTC](https://www.ruby-forum.com/t/functions-scope/89788/7 "2006-03-30T04:17:29Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Daniel B. wrote:

> Kernel::open, that’s what i was thinking of. Next time i’ll actually  
> _test_ stuff before I post an answer for someone ☹

Well now you know how :: works when there is nothing preceding it, and  
you know that OpenURI modifies the Kernel#open and  
Kernel::open methods. This is probably good thread for other folks to  
read also, don’t fear posting!

Zach  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.1 (GNU/Linux)  
Comment: Using GnuPG with Thunderbird - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFEK0AUMyx0fW1d8G0RAkS7AJ9zQIr+a8ZQDsVKOJ9uFBo7WlixJQCeKnUi  
NaZZAhEnYyjdtlYE5pkR2Ms=  
=1396  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Mirek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@Mirek](https://www.ruby-forum.com/u/Mirek)\
**Post date:** [March 29, 2006, 1:03pm UTC](https://www.ruby-forum.com/t/functions-scope/89788/8 "2006-03-29T13:03:59Z")

</div>

Mirek wrote:

> class C  
> def open()  
> # how to call global open function from here?  
> end  
> end

Kernel::open

lopex
