Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #1950

basic ENCAPSULATION help

X-FeedAbuse http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.16.109
Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!nospam.fr.eu.org!talisker.lacave.net!lacave.net!not-for-mail
From Kaye Ng <sbstn26@yahoo.com>
Newsgroups comp.lang.ruby
Subject basic ENCAPSULATION help
Date Wed, 30 Mar 2011 04:33:00 -0500
Organization Service de news de lacave.net
Lines 54
Message-ID <7ae35890c4342a71e3467ade701d3556@ruby-forum.com> (permalink)
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding Quoted-printable
X-Trace talisker.lacave.net 1301477855 56107 65.111.164.187 (30 Mar 2011 09:37:35 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Wed, 30 Mar 2011 09:37:35 +0000 (UTC)
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 380590
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <7ae35890c4342a71e3467ade701d3556@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:1950

Show key headers only | View raw


This example demonstrates the use of 'private'

class Person
  def initialize(name)
    set_name(name)
  end
  def name
    @first_name + ' ' + @last_name
  end
  private
  def set_name(name)
    first_name, last_name = name.split(/\s+/)
    set_first_name(first_name)
    set_last_name(last_name)
  end
  def set_first_name(name)
    @first_name = name
  end
  def set_last_name(name)
    @last_name = name
  end
end

From the book:
" private tells Ruby that any methods declared in this class from
there on should be kept private. This means that only code within the
object’s methods can
access those private methods, whereas code outside of the class cannot.
For example, this code no longer works "

p = Person.new("Fred Bloggs")
p.set_last_name("Smith")
__________________________________________________________________________
NoMethodError: private method 'set_last_name' called for
#<Person:0x337b68
@last_name="Bloggs", @first_name="Fred">
___________________________________________________________________________

When the author says, "only code within the object’s methods can
access those private methods, whereas code outside of the class
cannot.", what is the code in the example that can "access those private
methods", and what is the "code outside of the class that cannot" ?

That's it for now, more questions later.  Apologies to the author of
this book, it's not you, it's me.  I'm a really slow learner.

Thanks guys!

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextNext in thread | Find similar | Unroll thread


Thread

basic ENCAPSULATION help Kaye Ng <sbstn26@yahoo.com> - 2011-03-30 04:33 -0500
  Re: basic ENCAPSULATION help Gunther Diemant <g.diemant@gmx.net> - 2011-03-30 04:45 -0500
  Re: basic ENCAPSULATION help 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-30 21:20 -0500
    Re: basic ENCAPSULATION help 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-30 21:37 -0500

csiph-web