Professional Web Applications Themes

Accesing to private attributes - Ruby

Hi all, I was discusing about Object Oriented Languages with a friend and I have a question to do. Well, using Python, you can access to a private member of a class just prepending an underscore and the class name (i.e.: _myClass__myAttribute). It doesn't sound good to me. I don't like private attributes being accesible from outside. So, I'd like to know if you can do it on Ruby. I think no, that you must to use a method, but I'm in doubt. Thank you. -- Imobach González Sosa net org...

  1. #1

    Default Accesing to private attributes

    Hi all,

    I was discusing about Object Oriented Languages with a friend and I have a
    question to do. Well, using Python, you can access to a private member of a
    class just prepending an underscore and the class name (i.e.:
    _myClass__myAttribute). It doesn't sound good to me. I don't like private
    attributes being accesible from outside.

    So, I'd like to know if you can do it on Ruby. I think no, that you must to
    use a method, but I'm in doubt.

    Thank you.

    --
    Imobach González Sosa
    net
    org



    Imobach Guest

  2. #2

    Default Re: Accesing to private attributes

    You can do it.

    [dolio 05:55:13 ~]$ cat prac.rb
    #!/usr/bin/ruby

    class Foo
    attr_reader :bar
    end

    obj = Foo.new

    obj.instance_eval { bar = 3 }

    p obj.bar
    [dolio 05:55:17 ~]$ ./prac.rb
    3
    [dolio 05:55:23 ~]$

    However, I think that, generally, this isn't a bad thing. In both Ruby and
    Python, it's obvious what you're doing. You're not likely to type that in
    by accident. Hiding instance variables may be good programming practice,
    but that doesn't mean you shouldn't be able to get at them if it's
    absolutely
    necessary.

    You shouldn't use that kind of thing a lot, but you have to trust people not
    to be stupid.

    - Dan


    Dan Guest

  3. #3

    Default Re: Accesing to private attributes


    "Dan Doel" <cwru.edu> schrieb im Newsbeitrag
    news:cwru.edu... 

    or:

    irb(main):009:0> f=Foo.new
    => #<Foo:0x100dd198>
    irb(main):010:0> f.instance_variable_set :bar, "ha!"
    => "ha!"
    irb(main):011:0> f.instance_variable_get :bar
    => "ha!"
    irb(main):012:0>

    robert

    Robert Guest

  4. #4

    Default Re: Accesing to private attributes

    Dan Doel <cwru.edu> wrote in message news:<cwru.edu>... 

    Dan,

    Is there a safety level ( http://www.rubycentral.com/book/taint.html )
    that can prevent this methods from being used?
    Ged Guest

  5. #5

    Default Re: Accesing to private attributes

    Ged wrote:
     

    As far as I can tell, no.

    You could just undefine those methods. You can do that on a per-object
    basis:

    o = Object.new

    class << o
    undef :instance_eval
    undef :instance_variable_set
    undef :instance_variable_get
    # etc
    end

    o.instance_eval { a = 1 } #=> error


    Dan Guest

Similar Threads

  1. #39474 [NEW]: Object private attributes displays with list function
    By josefinarey at yahoo dot com dot ar in forum PHP Bugs
    Replies: 1
    Last Post: November 12th, 06:56 PM
  2. [7.4.1] signal 11 while accesing DB
    By Marcin in forum PostgreSQL / PGSQL
    Replies: 3
    Last Post: January 24th, 07:48 PM
  3. Replies: 0
    Last Post: July 31st, 07:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139