[#390749] Why are there so many similar/identical methods in core classes — Kassym Dorsel <k.dorsel@...>

Let's look at the Array class and start with method aliases.

14 messages 2011/12/02

[#390755] Inverse Operation of Module#include — Su Zhang <su.comp.lang.ruby@...>

Hi list,

21 messages 2011/12/02
[#390759] Re: Inverse Operation of Module#include — Ryan Davis <ryand-ruby@...> 2011/12/02

[#390764] Re: Inverse Operation of Module#include — Isaac Sanders <isaacbfsanders@...> 2011/12/02

I would suggest an Adapter pattern use here. IF there is something that has

[#390795] Is there a better way to check this array? — Wayne Brissette <wbrisett@...>

I have an array which contains items that need to be renamed. Unfortunately the way things are changed are based on a comparison of if something else exists. For example in this simple array:

9 messages 2011/12/03

[#390876] black magical hash element vivification — Chad Perrin <code@...>

Ruby (1.9.3p0 to be precise, installed with RVM) is not behaving as I

12 messages 2011/12/05

[#390918] WEB SURVEY about Ruby Community — Intransition <transfire@...>

Did any one else get this survey request?

14 messages 2011/12/07

[#390972] Which is the best online document conversion site? — "Maria M." <mariamoore952@...>

Can anyone tell me that which is the best online document conversion

7 messages 2011/12/08

[#390976] Confusing results from string multiplication — Rob Marshall <robmarshall@...>

Hi,

19 messages 2011/12/08

[#391019] How can I do h["foo"] += "bar" if h["foo"] does not exist? — "Andrew S." <andrewinfosec@...>

Hi there,

13 messages 2011/12/09

[#391027] reading from file without end-of-lines — Janko Muzykant <umrzykus@...>

hi,

20 messages 2011/12/09
[#391028] Re: reading from file without end-of-lines — Gavin Sinclair <gsinclair@...> 2011/12/09

> i'm trying to read a few text values from single file:

[#391031] Re: reading from file without end-of-lines — Robert Klemme <shortcutter@...> 2011/12/09

On Fri, Dec 9, 2011 at 9:58 AM, Gavin Sinclair <gsinclair@gmail.com> wrote:

[#391042] Re: reading from file without end-of-lines — Gavin Sinclair <gsinclair@...> 2011/12/09

On Fri, Dec 9, 2011 at 8:18 PM, Robert Klemme

[#391135] I need advice on what to do next. — Nathan Kossaeth <system_freak_2004@...>

I am new to programming. I read the ebook "Learn to Program" by Chris

23 messages 2011/12/12

[#391216] perf optimization using profile results — Chuck Remes <cremes.devlist@...>

I need some help with optimizing a set of libraries that I use. They are ffi-rzmq, zmqmachine and rzmq_brokers (all up on github).

13 messages 2011/12/13
[#391218] Re: perf optimization using profile results — Chuck Remes <cremes.devlist@...> 2011/12/13

On Dec 13, 2011, at 9:57 AM, Chuck Remes wrote:

[#391234] Re: perf optimization using profile results — Charles Oliver Nutter <headius@...> 2011/12/14

A couple quick observations.

[#391238] Re: perf optimization using profile results — Chuck Remes <cremes.devlist@...> 2011/12/14

On Dec 13, 2011, at 7:03 PM, Charles Oliver Nutter wrote:

[#391324] ruby 1.9 threading performance goes non-linear — Joel VanderWerf <joelvanderwerf@...>

12 messages 2011/12/16
[#391325] Re: ruby 1.9 threading performance goes non-linear — Eric Wong <normalperson@...> 2011/12/16

Joel VanderWerf <joelvanderwerf@gmail.com> wrote:

[#391420] Accessing class instance variables from an instance? — "Shareef J." <shareef@...>

Hi there,

26 messages 2011/12/20
[#391454] Re: Accessing class instance variables from an instance? — Khat Harr <myphatproxy@...> 2011/12/21

Actually, now that I'm thinking about it the existing behavior sort of

[#391456] Re: Accessing class instance variables from an instance? — Josh Cheek <josh.cheek@...> 2011/12/21

On Tue, Dec 20, 2011 at 9:42 PM, Khat Harr <myphatproxy@hotmail.com> wrote:

[#391545] Kernel#exit raises an exception? — Khat Harr <myphatproxy@...>

While I was working on embedding an interpreter I wrote a function to

13 messages 2011/12/24

[#391618] rvmsh: An easy installer for RVM — Bryan Dunsmore <dunsmoreb@...>

I have recently begun work on a project called [rvmsh]

12 messages 2011/12/29

[#391783] Mailspam — Gunther Diemant <g.diemant@...>

Is there a way to stop this mailspam of Luca (Mail)?

12 messages 2011/12/29

[#391790] What’s the standard way of implementing #hash for value objects in Ruby? — Nikolai Weibull <now@...>

Hi!

23 messages 2011/12/29
[#391792] Re: What’s the standard way of implementing #hash for value objects in Ruby? — Gunther Diemant <g.diemant@...> 2011/12/29

I think you can't access instance variables from a class method, so

[#391793] Re: What’s the standard way of implementing #hash for value objects in Ruby? — Nikolai Weibull <now@...> 2011/12/29

On Thu, Dec 29, 2011 at 15:52, Gunther Diemant <g.diemant@gmx.net> wrote:

[#391811] Re: What’s the standard way of implementing #hash for value objects in Ruby? — Robert Klemme <shortcutter@...> 2011/12/29

On Thu, Dec 29, 2011 at 4:06 PM, Nikolai Weibull <now@bitwi.se> wrote:

[#391812] Re: What’s the standard way of implementing #hash for value objects in Ruby? — Nikolai Weibull <now@...> 2011/12/29

On Fri, Dec 30, 2011 at 00:26, Robert Klemme <shortcutter@googlemail.com> wrote:

[#391816] Re: What’s the standard way of implementing #hash for value objects in Ruby? — Josh Cheek <josh.cheek@...> 2011/12/30

On Thu, Dec 29, 2011 at 5:47 PM, Nikolai Weibull <now@bitwi.se> wrote:

[#391833] Re: What’s the standard way of implementing #hash for value objects in Ruby? — Robert Klemme <shortcutter@...> 2011/12/30

On Fri, Dec 30, 2011 at 12:47 AM, Nikolai Weibull <now@bitwi.se> wrote:

Re: Accessing class instance variables from an instance?

From: Robert Klemme <shortcutter@...>
Date: 2011-12-22 10:41:37 UTC
List: ruby-talk #391505
On Thu, Dec 22, 2011 at 2:09 AM, Garthy D
<garthy_lmkltybr@entropicsoftware.com> wrote:
> I've run into this kind of thing before, and it does impact me regularly.
> Having said that, I know my C++ background (which allows this) biases me
> somewhat on the issue. ;)
>
> I tend to steer away from "Foo.bar" style calls inside class Foo for the
> following reason- it complicates refactoring. If you determine that a class
> would benefit from being split (perhaps because another class has common
> functionality and you want it to share a common base), then you have to
> selectively rename all of the calls you have made to point to the correct
> class. Sure, it's not hard, and doesn't take too long, but when you're
> refactoring something for the sake of something else you have developed, not
> having to deal with additional chores to make it work makes it less likely
> that you'll stuff something up in the process. A similar problem arises with
> renaming the class, but admittedly the solution tends to be far easier.
>
> For that reason, I'd instead use "class.bar" calls, if it were possible,
> which it is not, which brings us to "self.class.bar". I do this as you can't
> always tell ahead of time when you'll need to refactor something to pull out
> a base class, and so I'll assume that (almost) all of the classes I work on
> could be subject to such changes later in its lifetime. If you could tell
> beforehand, you would have designed it that way in the first place. :) Now,
> if you have a class method that is used both inside and out, but mostly
> inside (and regularly), it's a pain to have to write "self.class.bar" each
> time you want to call "bar". Incidentally, for "bar", I have a specific
> method in mind for something I have been working on (ie. a real-world
> problem), that makes the most sense as a class method, since it works
> entirely off the arguments and carries no state. The call in question has
> very specific structured input requirements, and is called extremely
> frequently inside the class, and occasionally outside.
>
> In my case, I ended up solving it by adding an "abar" method that just
> called "self.class.bar", although using it that way does feel a bit silly,
> and makes the code less readable (an observer might ask what the difference
> between "bar" and "abar" is, for example).

A better alternative would be to do this:

class Object
  def my_class; self.class end
end

Then you do not need to do

Foo.bar
self.class.bar

but instead you can do

my_class.bar

which seems a lot clearer to me while avoiding the nasty "self.class".
 Maybe someone else can think of a better (presumably: shorter) name
than #my_class.

> Anyway, I just thought I'd share my particular experience. It's not hard to
> work around, but there are definitely good reasons to want to make it work
> that way. It falls under the area of a "quirk" for me, but that is of course
> just my personal experience based on my personal experience.

I guess my main point is that making a method call appear to be on the
local instance which in fact is done on another instance is
misleading.  For the reader it is better to at least signal that #foo
is not invoked on self but rather on another instance.  For that you
need a method or a variable reference.

>> That has the added advantage that
>> you make the call explicit in instance methods which use it and it is
>> immediately clear that this method does not manipulate instance state.
>
> There are definitely cases where this could be advantageous, but there are
> also cases where this is not necessary or desirable. In general, I would
> personally say that if a particular mechanism is sometimes beneficial, but
> sometimes not, it is better to leave it up to the person writing the code to
> emphasise the mechanism where it is appropriate, and de-emphasise where it
> is not. For example, sometimes it is beneficial to be specific about the
> particular class of an object (or its capabilities), but would that justify
> requiring every method call to be prefixed by the class name, every time? I
> would say not. There are times where being specific about it would be
> beneficial, but in the remaining cases it would clutter up the code
> unnecessarily.

We differ in the estimation about the distribution of useful and
harmful.  I'd say obfuscating a method is invoked on another instance
does more harm than good so the default should be to not make it too
easy.  This forces people to know what they are doing when they do it.
 As an example: you can modify instance variables of another instance
but it is deliberately made clumsy (#set_instance_variable etc.)
although possible.

> I think in the situation I outlined above, and possibly also in Josh's case,
> it falls in the realm of clutter. This does not, of course, take away from
> the fact that in some situations it would be important, or absolutely
> essential, to explicitly convey that the instance state will not be
> manipulated. I know that I have certainly run into this case as well- I
> won't bother with a personal anecdote as I suspect you also already know of
> many cases where it is extremely beneficial already. :)

That's the point: OO is mostly about the state of instances and its
changes.  Everything that helps keep clear what's happening is good
IMHO.

> I guess what I am saying is that if something is sometimes beneficial, and
> sometimes detrimental, that enforcing it is not always the best idea, but
> providing an optional means to make it explicit is beneficial. I do not
> believe that is a good idea in general to either force a mechanism that is
> only sometimes useful, or argue that as the mechanism is sometimes
> important, that having a means to conveniently avoid the mechanism when it
> is detrimental is not justified. There are of course always all sorts of
> tradeoffs when designing a language- perhaps the particular feature Josh and
> I would find beneficial is not readily possible for some technical reason.

There are no technical obstacles here defining instance methods which
access class state.  I just happen to believe that the tradeoff fall
on the side that we do not want to have such a feature because the
convenience of the writer is the pain of the reader of the code.

> All IMHO.

+1

> PS. Wow, that message was a bit longer than I had planned. ;)

:-)

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

In This Thread