PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > private class methods- functionality or shortcoming?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
private class methods- functionality or shortcoming?

Réponse
 
LinkBack Outils de la discussion
Vieux 09/06/2008, 15h01   #1
amit saxena
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut private class methods- functionality or shortcoming?

[Note: parts of this message were removed to make it a legal post.]

I just switched to Ruby and came across it.
If we have to make a class method private then we should write it as:

class Abc
   
    class<<self
        private
        def fun
            p "hello"
        end
    end
   
end

But if we write as:

class Abc
   
    private
    def self.fun
        p "hello"
    end

end

then the class method is not private but behaves as public.

So, is it a carefully designed functionality? If so, then what is the thinking behind it?
OR any other reasons behind it?
Somebody told me that ruby encourages to define class methods the first way.
But I think that if you are giving more than one methods to do a thing, then all should function the same way.

Thanks and Regards,
Amit




  Réponse avec citation
Vieux 09/06/2008, 15h09   #2
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private class methods- functionality or shortcoming?

On Monday 09 June 2008, amit saxena wrote:
> I just switched to Ruby and came across it.
> If we have to make a class method private then we should write it as:
>
> class Abc
>    
>     class<<self
>         private
>         def fun
>             p "hello"
>         end
>     end
>    
> end
>
> But if we write as:
>
> class Abc
>    
>     private
>     def self.fun
>         p "hello"
>     end
>
> end
>
> then the class method is not private but behaves as public.
>
> So, is it a carefully designed functionality? If so, then what is the
> thinking behind it? OR any other reasons behind it?
> Somebody told me that ruby encourages to define class methods the first
> way. But I think that if you are giving more than one methods to do a
> thing, then all should function the same way.
>
> Thanks and Regards,
> Amit


The Module#private method only works for instance methods, not for class
method. To make a class method private, you should use the
Module#private_class_method method. Note that, unlike private,
private_class_method only accepts one argument, so that you want to make more
than one method private, you should call it multiple times.

So, the correct method to make the method private using the second form is:

class Abc

def self.fun
p "hello"
end
private_class_method :fun

end

I hope this s

Stefano

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h31.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10966 seconds with 10 queries