Computer Science Canada

[TYS] Visibility modifiers

Author:  wtd [ Sun Nov 26, 2006 4:29 pm ]
Post subject:  [TYS] Visibility modifiers

What aspect of Ruby's design makes modifiers like public, private and protected, though present, much less necessary than in languages such as Java, C++, and C#?

What important object-oriented concept am I hinting at?

Author:  ericfourfour [ Tue Jan 09, 2007 11:32 pm ]
Post subject:  Re: [TYS] Visibility modifiers

I realize its been around a month and a half but I thought I could give it a shot.

They're much less necessary because Ruby is dynamically somthing-or-other. I think I read somewhere that these are determined while the program is running and can be changed. The only time you will get an access violation in Ruby is if you call the method.

In other languages they are determined at compile-time. You will receive the access violation at compile-time as well.

Author:  wtd [ Wed Jan 10, 2007 2:25 am ]
Post subject:  RE:[TYS] Visibility modifiers

Not the answer I was looking for, actually.

Since it's been awhile, I'll post the answer.

I was looking for you to talk about encapsulation. In a typical Java, C++, or C# program, instance variables will all be declared private. At the same time, most if not all of your methods will be public.

In Ruby, this is the default behavior. Instance variables are always private, and methods are by default public.

Author:  ericfourfour [ Wed Jan 10, 2007 4:02 pm ]
Post subject:  Re: [TYS] Visibility modifiers

For curiosities sake, is there an advantage to have the instance variables not private and the methods not public by default in languages like Java, C++, or C#?

Author:  wtd [ Wed Jan 10, 2007 6:12 pm ]
Post subject:  RE:[TYS] Visibility modifiers

It offers choice, and flexibility. Ruby's convenience comes at the cost of that, to some degree. The language's designer clearly thought it a cost worth uncurring.

Author:  Clayton [ Wed Jan 10, 2007 6:22 pm ]
Post subject:  Re: [TYS] Visibility modifiers

although you can declare a method protected if I'm not mistaken?

Author:  wtd [ Wed Jan 10, 2007 8:04 pm ]
Post subject:  RE:[TYS] Visibility modifiers

Yes, methods can be public, private, or protected.

Instance variables, though, are always private.

Author:  ericfourfour [ Wed Jan 10, 2007 8:45 pm ]
Post subject:  Re: [TYS] Visibility modifiers

wtd, you just confused me. If instance variables are always private how come this works?

Ruby:
class Foo
  def initialize(baz)
    @baz = baz
  end
end

class FooBar < Foo
  def to_s
    "#{@baz}"
  end
end

puts FooBar.new("Hello World!")


Or am I missing something?

Author:  rdrake [ Wed Jan 10, 2007 9:07 pm ]
Post subject:  RE:[TYS] Visibility modifiers

Freakman, it's a case of "daddy only lets his children touch his privates," really.

Author:  Clayton [ Wed Jan 10, 2007 9:16 pm ]
Post subject:  Re: [TYS] Visibility modifiers

ericfourfour: because the child class inherits Foo, it gains everything that was inside of the Foo class, including instance variables.

rdrake: I think you meant to point that at ericfourfour Razz

Author:  wtd [ Wed Jan 10, 2007 9:21 pm ]
Post subject:  RE:[TYS] Visibility modifiers

The thing ericfourfour is wondering about is why this works, since in other languages, private variables and methods may not be accessed in subclasses.

I suspect I am moderately mistaken, and instance variables are in effect protected in Ruby. In terms of what this makes possible in such a highly dynamic programming language, this is a good thing.


: