Module "re-opening"

Hi there, first time arond here for me, and first step with ruby ^^

well, i come with a simple (maybe stupid) question:

http://sprunge.us/BjJP?rb

why this simple source doesn’t do what is expected? (modify the
visibility of sqrt module)

thx in advance for any answer ^^

bye
M

tilde wrote:

bye
M

i mean, sqrt method, sorry

On Wed, Apr 21, 2010 at 7:55 AM, tilde [email protected]
wrote:

Hi there, first time arond here for me, and first step with ruby ^^

well, i come with a simple (maybe stupid) question:

http://sprunge.us/BjJP?rb

(copying it here since it’s small):

1 #! /usr/bin/env ruby1.9
2
3 module Math
4 private :sqrt
5 end
6
7 puts Math.sqrt(9)

why this simple source doesn’t do what is expected? (modify the visibility
of sqrt module)

Because sqrt is a module method:

irb(main):001:0> module Math
irb(main):002:1> class << self
irb(main):003:2> private :sqrt
irb(main):004:2> end
irb(main):005:1> end
=> #Class:Math
irb(main):006:0> Math.sqrt(9)
NoMethodError: private method `sqrt’ called for Math:Module
from (irb):6
from :0

Jesus.

On Apr 20, 2010, at 22:55 , tilde wrote:

Hi there, first time arond here for me, and first step with ruby ^^

well, i come with a simple (maybe stupid) question:

http://sprunge.us/BjJP?rb

why this simple source doesn’t do what is expected? (modify the visibility of sqrt module)

thx in advance for any answer ^^

It is essentially a class method, so you need to invoke private at the
class level:

module Math; class << self; private :sqrt; end; end
Math.sqrt 42
NoMethodError: private method `sqrt’ called for Math:Module

Ryan D. wrote:

thx in advance for any answer ^^

It is essentially a class method, so you need to invoke private at the class level:

module Math; class << self; private :sqrt; end; end
Math.sqrt 42
NoMethodError: private method `sqrt’ called for Math:Module

I see… thx to all ^^

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs