
-----------------------------------
wtd
Sun Nov 26, 2006 4:29 pm

[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?

-----------------------------------
ericfourfour
Tue Jan 09, 2007 11:32 pm

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.

-----------------------------------
wtd
Wed Jan 10, 2007 2:25 am

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.

-----------------------------------
ericfourfour
Wed Jan 10, 2007 4:02 pm

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#?

-----------------------------------
wtd
Wed Jan 10, 2007 6:12 pm

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.

-----------------------------------
Clayton
Wed Jan 10, 2007 6:22 pm

Re: [TYS] Visibility modifiers
-----------------------------------
although you can declare a method protected if I'm not mistaken?

-----------------------------------
wtd
Wed Jan 10, 2007 8:04 pm

RE:[TYS] Visibility modifiers
-----------------------------------
Yes, methods can be public, private, or protected.

Instance variables, though, are always private.

-----------------------------------
ericfourfour
Wed Jan 10, 2007 8:45 pm

Re: [TYS] Visibility modifiers
-----------------------------------
wtd, you just confused me. If instance variables are always private how come this works?

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?

-----------------------------------
rdrake
Wed Jan 10, 2007 9:07 pm

RE:[TYS] Visibility modifiers
-----------------------------------
Freakman, it's a case of "daddy only lets his children touch his privates," really.

-----------------------------------
Clayton
Wed Jan 10, 2007 9:16 pm

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 :P

-----------------------------------
wtd
Wed Jan 10, 2007 9:21 pm

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.
