Attr Methods and object setters

Hi --

Gregory Brown wrote:

Anyway, here's a better way to solve the problem that probably
addresses your concerns:

def name(*args)
return @name if args.empty?
@name = args.first
end

Another way to do this is to use some kind of singleton object

Nothing = Object.new
def name(value = Nothing)
@name = value unless value == Nothing
@name
end

At the risk of someone saying that I've made it too cryptic, it could
be one line shorter:

   Nothing = Object.new
   def name(value = Nothing)
     @name = value unless value == Nothing
   end

David

···

On Sun, 28 Jun 2009, Gregory Brown wrote:

On Sat, Jun 27, 2009 at 11:26 PM, Daniel DeLorme<dan-ml@dan42.com> wrote:

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2\)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com

David Masover wrote:

···

On Saturday 27 June 2009 10:26:51 pm Daniel DeLorme wrote:

Nothing = Object.new
def name(value = Nothing)
   @name = value unless value == Nothing
   @name
end

Ok, I guess you have to be trying, but I can still break this:

class Everything
  def == other
    true
  end
end

name Everything.new

And the way to fix it:

Nothing = Object.new
def name(value = Nothing)
  @name = value unless Nothing == value
  @name
end

I believe that "same object" is slightly better expressed by:

  Nothing.equal? value

although in this case, for an instance of Object, it should be the same.
--
Posted via http://www.ruby-forum.com/\.

Interesting. Does this really matter though?
I know that GC is slow in Ruby but just how soon would this become a
problem in practical applications.

(I suppose it depends on the application)

-greg

···

On Sat, Jun 27, 2009 at 8:41 PM, Joel VanderWerf<vjoel@path.berkeley.edu> wrote:

Gregory Brown wrote:

On Sat, Jun 27, 2009 at 4:52 PM, Fabian >> Streitel<karottenreibe@googlemail.com> wrote:

...

def name(*args)
return @name if args.empty?
@name = args.first
end

Better, but IMHO that's WAY too much overhead for something as basic as a
setter.
After all, you have to construct an array everytime you access the
setter...

Huh? This is the way Ruby arguments work no matter what. Using *args
just gives you raw access to the arguments.

No, *args does construct an array.

I'd probably use Daniel's method.

I've found Object.new to create a null value(not to be confused with
nil) or sentinel value, quite useful at times, in both Ruby and
Smalltalk.

Basically this gives you an object which is useful (only) because it
has a unique identity and is guaranteed not to appear by chance.

···

On Sun, Jun 28, 2009 at 9:01 AM, Fabian Streitel<karottenreibe@googlemail.com> wrote:

If you like that method better -- noones keeping you from using it. And I
admit,
for DSLs it is pretty nice. I'd use it there.

Nothing = Object.new
def name(value = Nothing)
@name = value unless value == Nothing
@name
end

Or Daniel's method.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Oh, I'm not sure if we crossed threads at some point, but I was
suggesting using that approach *only* for DSLs.
So we are totally in agreement.

-greg

···

On Sun, Jun 28, 2009 at 9:01 AM, Fabian Streitel<karottenreibe@googlemail.com> wrote:

But for any normal class that does not provide DSL semantics, I'd still go
with normal
getters and setters. I just don't see the point of reducing the amount of my
typing by
a single =, when on the other hand I have to either construct a whole array
each time
or introduce a new neutral element.

To mee that just doesn't feel right, I guess...

Actually the second line IS necessary

Nothing = Object.new
def name(value = Nothing)
   @name = value unless value == Nothing
end

name 42 # => 42
name # => nil

···

On Sun, Jun 28, 2009 at 10:18 AM, David A. Black<dblack@rubypal.com> wrote:

At the risk of someone saying that I've made it too cryptic, it could
be one line shorter:

Nothing = Object.new
def name(value = Nothing)
@name = value unless value == Nothing
end

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Hi --

···

On Sun, 28 Jun 2009, Rick DeNatale wrote:

On Sun, Jun 28, 2009 at 10:18 AM, David A. Black<dblack@rubypal.com> wrote:

At the risk of someone saying that I've made it too cryptic, it could
be one line shorter:

Nothing = Object.new
def name(value = Nothing)
@name = value unless value == Nothing
end

Actually the second line IS necessary

Nothing = Object.new
def name(value = Nothing)
  @name = value unless value == Nothing
end

name 42 # => 42
name # => nil

Whoops -- right you are. Rewind.

(At least you didn't say it was cryptic :slight_smile:

David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2\)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com

Oh, I'm not sure if we crossed threads at some point, but I was
suggesting using that approach *only* for DSLs.
So we are totally in agreement.

alright then! my bad...
I thought you meant this to be generally an alternative to the traditional
setters.

Greetz!
k

···

2009/6/28 Gregory Brown <gregory.t.brown@gmail.com>

On Sun, Jun 28, 2009 at 9:01 AM, Fabian > Streitel<karottenreibe@googlemail.com> wrote:

> But for any normal class that does not provide DSL semantics, I'd still
go
> with normal
> getters and setters. I just don't see the point of reducing the amount of
my
> typing by
> a single =, when on the other hand I have to either construct a whole
array
> each time
> or introduce a new neutral element.
>
> To mee that just doesn't feel right, I guess...

Oh, I'm not sure if we crossed threads at some point, but I was
suggesting using that approach *only* for DSLs.
So we are totally in agreement.

-greg