From: tilde on
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
From: tilde on
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 ^^
>
> bye
> M

i mean, sqrt _method_, sorry
From: Jesús Gabriel y Galán on
On Wed, Apr 21, 2010 at 7:55 AM, tilde <tildeNO(a)spamautistici.org> 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.

From: Ryan Davis on

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



From: tilde on
Ryan Davis wrote:
> 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
>
>
>

I see.. thx to all ^^