Question on TagList class

33 views
Skip to first unread message

Petros Papadopoulos

unread,
Oct 26, 2014, 3:30:40 AM10/26/14
to objects-...@googlegroups.com
Hi guys,

On page 121 of the book where the TagList is defined, I've noticed that #to_s calls the 'tags' accessor.

What the difference would be if '@tags' were called instead?


Thanks,

Petros


Marten Veldthuis

unread,
Oct 26, 2014, 6:01:31 AM10/26/14
to Petros Papadopoulos, objects-...@googlegroups.com

I think Avdi himself explained the advantages to using accessors very well on RubyTapas: Ruby Tapas | #4: Barewords. In summary, accessors are pretty cheap as far as abstractions go, but they give you the freedom to change the implementation of ‘tags’. Maybe the data structure for tags changes, and instead of an array, it now uses a hash internally. If you reference @tags everywhere, you now have multiple places to fix. If you used an accessor, just turn it into a ‘def tags; @tags.keys; end’.

--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Petros Papadopoulos

unread,
Oct 26, 2014, 7:18:28 AM10/26/14
to objects-...@googlegroups.com, piet...@gmail.com
Thanks Marten for taking the time to reply! Well explained I may say but I am tempted to wonder then why has he not used 'tags' instead of '@tags' in #to_ary

Just for reference:

def to_s
  tags
.join(", ")
end
def to_ary
 
@tags
end

Thanks again!

Dave Newton

unread,
Oct 26, 2014, 11:40:56 AM10/26/14
to objects-...@googlegroups.com, piet...@gmail.com

I'm guessing, but probably "because humans" ;-)

Avdi Grimm

unread,
Oct 26, 2014, 5:28:31 PM10/26/14
to objects-...@googlegroups.com

On Sun, Oct 26, 2014 at 11:40 AM, Dave Newton <davel...@gmail.com> wrote:
I'm guessing, but probably "because humans" ;-)

:-)

--
Avdi Grimm
http://avdi.org

Matt Wynne

unread,
Oct 27, 2014, 8:30:10 AM10/27/14
to objects-...@googlegroups.com, Petros Papadopoulos
Sandi Metz recently explained this on the POODL training we did a couple of months ago, but from a different perspective:

IIRC she said “I want you to think about sending a message, not just about grabbing some data”.

cheers,

signature.asc
Reply all
Reply to author
Forward
0 new messages