Functions scope

D

Daniel Baird

------=_Part_10173_2490881.1143615532846
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

i think you can do

::eek:pen

;D

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


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

------=_Part_10173_2490881.1143615532846--
 
Z

zdennis

Mirek said:
require 'open-uri'

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

o = C.new
o.open('x')

---

open.rb:6: syntax error, unexpected tIDENTIFIER, expecting tCONSTANT
::eek:pen(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
 
1

13

Hi,

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

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

Daniel Baird

------=_Part_11372_4660733.1143680217042
Content-Type: text/plain; charset=ISO-8859-2
Content-Transfer-Encoding: base64
Content-Disposition: inline

S2VybmVsOjpvcGVuLCAgdGhhdCdzIHdoYXQgaSB3YXMgdGhpbmtpbmcgb2YuICBOZXh0IHRpbWUg
aSdsbCBhY3R1YWxseQoqdGVzdCogc3R1ZmYgYmVmb3JlIEkgcG9zdCBhbiBhbnN3ZXIgZm9yIHNv
bWVvbmUgOigKCjtEYW5pZWwKCk9uIDI5LzAzLzA2LCBNYXJjaW4gTWllbL958XNraSA8bG9wZXh4
QGF1dG9ncmFmLnBsPiB3cm90ZToKPgo+IE1pcmVrIHdyb3RlOgo+ID4gY2xhc3MgQwo+ID4gICBk
ZWYgb3BlbigpCj4gPiAgICAgIyBob3cgdG8gY2FsbCBnbG9iYWwgb3BlbiBmdW5jdGlvbiBmcm9t
IGhlcmU/Cj4gPiAgIGVuZAo+ID4gZW5kCj4gPgo+Cj4gS2VybmVsOjpvcGVuCj4KPgo+IGxvcGV4
Cj4KPgoKCi0tCkRhbmllbCBCYWlyZApodHRwOi8vZGFuaWVsYmFpcmQuY29tIChUaWRkbHlXO25r
cyEgOjogV2hpdGVib2FyZCBLb2FsYSA6OiBCbG9nIDo6IFRoaW5ncwpUaGF0IFN1Y2spCltbTXkg
d2ViaG9zdCB1cHRpbWUgaXMgfiA5MiUuLiBpZiBubyBhbnN3ZXIgcGxzIGNhbGwgYWdhaW4gbGF0
ZXIhXV0K
------=_Part_11372_4660733.1143680217042--
 
Z

zdennis

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

Daniel said:
Kernel::eek:pen, 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::eek:pen 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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,062
Members
47,669
Latest member
johnmaxwell

Latest Threads

Top