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
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
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