[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Two Questions about Hash Defaults

Pete Elmore

4/2/2005 1:28:00 AM

I have a couple of questions about hashes. The first is fairly simple:
is there a way to set a default so that when the value for a key is
changed, the object it points to is default.dup instead of default? So
that if I do something like this:

lists = Hash.new([])
lists[:a].push 1
lists[:b].push 2
# Then doing the following
p lists[:a]
# Gets me
=> [1]
# Instead of
=> [1, 2]

And 'lists' is '{:a => [1], :b => [2]}' instead of '{}' with a default
of '[1, 2]'.

I know why it happens this way, but is it possible to get it to behave
differently? So that instead of
lists[key] = [] if lists[key].nil? # Assuming default is nil
lists[key].push value
or
lists[key] = lists[key].dup.push value # default is []
I can write simply
lists[key].push value # default is []

The second question is about setting defaults from one hash based on
another. This question is more along the lines of 'Does this code
violate Ruby sensibilities?'

class Hash
def add_defaults(default_hash)
raise TypeError unless default_hash.kind_of? Hash
default_hash.each { |k,v|
self[k] = v unless self.has_key? k
}
self
end
end

def some_function(str, options=Hash.new(nil))
# Set the defaults
defaults = {
:preserve_breaks => true,
:indent => 0,
:columns => 79,
}
options.add_defaults defaults
end

some_function("asdf", { :preserve_breaks => false })


21 Answers

Yukihiro Matsumoto

4/2/2005 2:11:00 AM

0

Hi,

In message "Re: Two Questions about Hash Defaults"
on Sat, 2 Apr 2005 10:28:12 +0900, Pete Elmore <pete@petta-tech.com> writes:
|
|I have a couple of questions about hashes. The first is fairly simple:
|is there a way to set a default so that when the value for a key is
|changed, the object it points to is default.dup instead of default?

list = Hash.new{[]}

|The second question is about setting defaults from one hash based on
|another. This question is more along the lines of 'Does this code
|violate Ruby sensibilities?'

defaults = {
:preserve_breaks => true,
:indent => 0,
:columns => 79,
}
list = Hash.new{|h,k|defaults[k]}


ES

4/2/2005 2:24:00 AM

0


Con fecha 2/4/2005, "Pete Elmore" <pete@petta-tech.com> escribió:
>I have a couple of questions about hashes. The first is fairly simple:
>is there a way to set a default so that when the value for a key is
>changed, the object it points to is default.dup instead of default? So
>that if I do something like this:
>
>lists = Hash.new([])
>lists[:a].push 1
>lists[:b].push 2
># Then doing the following
>p lists[:a]
># Gets me
>=> [1]
># Instead of
>=> [1, 2]
>
>And 'lists' is '{:a => [1], :b => [2]}' instead of '{}' with a default
>of '[1, 2]'.
>
>I know why it happens this way, but is it possible to get it to behave
>differently? So that instead of
> lists[key] = [] if lists[key].nil? # Assuming default is nil
> lists[key].push value
>or
> lists[key] = lists[key].dup.push value # default is []
>I can write simply
> lists[key].push value # default is []
>
>The second question is about setting defaults from one hash based on
>another. This question is more along the lines of 'Does this code
>violate Ruby sensibilities?'
>
>class Hash
> def add_defaults(default_hash)
> raise TypeError unless default_hash.kind_of? Hash
> default_hash.each { |k,v|
> self[k] = v unless self.has_key? k
> }
> self
> end
>end
>
>def some_function(str, options=Hash.new(nil))
> # Set the defaults
> defaults = {
> :preserve_breaks => true,
> :indent => 0,
> :columns => 79,
> }
> options.add_defaults defaults
>end
>
>some_function("asdf", { :preserve_breaks => false })

Looks like matz already provided you with a good answer,
but here's a different take since I am not sure if you
are indeed referring to default values as the language
defines them or 'just' default values for your application
logic:

def some_function(str, options = {})
defaults = {:preserve => true, :indent => 0, :columns =>79}
options = defaults.merge options
end



Robert Klemme

4/2/2005 7:39:00 AM

0


"Pete Elmore" <pete@petta-tech.com> schrieb im Newsbeitrag
news:424DCDEA.5020603@petta-tech.com...
>I have a couple of questions about hashes. The first is fairly simple: is
>there a way to set a default so that when the value for a key is changed,
>the object it points to is default.dup instead of default? So that if I do
>something like this:
>
> lists = Hash.new([])

You want this:

lists = Hash.new {|h,k| h[k]=[]}

> lists[:a].push 1
> lists[:b].push 2
> # Then doing the following
> p lists[:a]
> # Gets me
> => [1]
> # Instead of
> => [1, 2]
>
> And 'lists' is '{:a => [1], :b => [2]}' instead of '{}' with a default of
> '[1, 2]'.
>
> I know why it happens this way, but is it possible to get it to behave
> differently? So that instead of
> lists[key] = [] if lists[key].nil? # Assuming default is nil
> lists[key].push value
> or
> lists[key] = lists[key].dup.push value # default is []
> I can write simply
> lists[key].push value # default is []
>
> The second question is about setting defaults from one hash based on
> another. This question is more along the lines of 'Does this code violate
> Ruby sensibilities?'
>
> class Hash
> def add_defaults(default_hash)
> raise TypeError unless default_hash.kind_of? Hash
> default_hash.each { |k,v|
> self[k] = v unless self.has_key? k
> }
> self
> end
> end
>
> def some_function(str, options=Hash.new(nil))
> # Set the defaults
> defaults = {
> :preserve_breaks => true,
> :indent => 0,
> :columns => 79,
> }

> options.add_defaults defaults

This should do:
options = defaults.update options

> end
>
> some_function("asdf", { :preserve_breaks => false })

Kind regards

robert

Pete Elmore

4/4/2005 5:51:00 PM

0

Yukihiro Matsumoto wrote:
> Hi,
Wow, Matz himself! :)
> list = Hash.new{[]}
> defaults = {
> :preserve_breaks => true,
> :indent => 0,
> :columns => 79,
> }
> list = Hash.new{|h,k|defaults[k]}
This is interesting; thanks for the response!



Spencer L.

1/5/2010 10:23:00 PM

0


"Nutella" <amongthebelievers@yahoo.com> wrote in message
news:ea1e9746-5e18-4edd-83cf-112eb750abf5@a15g2000yqm.googlegroups.com...
On Jan 5, 10:00 am, BLACKPOOLJIMMY <Chippandf...@aol.com> wrote:

> The artist/musician has previously voiced concerns about writing a
> book, because it may upset the other Beatles' families.
> In a 2007 interview, she said: "There are things that I can?t write
> because it may hurt someone. I think about how it might hurt (their)
> children, and I don?t want to do that.?


Will the book be titled: "THE WEDGE"?

My thoughts:

It's HER autobiography, yet she's worried about upsetting Beatles'
family members.
WHY? She has been alive for a very long time, and only actually around
The Beatles
while they were a group for about two years! Perhaps, if she
concentrates on HER
autobiography, and not on The Beatles and their families
'biographies', then no one
would care. Obviously, for this to be interesting, she has to make it
about 90% Beatles/John
related, otherwise, no one would buy it.

That goes to show you the REAL Yoko Ono life is not all that
interesting. It's only the Beatle stuff
that makes her life interesting. And she needs to include all the
stuff that will make the Beatles families
angry because otherwise, it wouldn't get picked-up by a publisher.

Am I wrong?

You are absolutely right....sad to say.


RichL

1/6/2010 12:03:00 AM

0

Nutella <amongthebelievers@yahoo.com> wrote:
> On Jan 5, 10:00 am, BLACKPOOLJIMMY <Chippandf...@aol.com> wrote:
>
>> The artist/musician has previously voiced concerns about writing a
>> book, because it may upset the other Beatles' families.
>> In a 2007 interview, she said: "There are things that I can?t write
>> because it may hurt someone. I think about how it might hurt (their)
>> children, and I don?t want to do that.?
>
>
> Will the book be titled: "THE WEDGE"?
>
> My thoughts:
>
> It's HER autobiography, yet she's worried about upsetting Beatles'
> family members.
> WHY? She has been alive for a very long time, and only actually around
> The Beatles
> while they were a group for about two years! Perhaps, if she
> concentrates on HER
> autobiography, and not on The Beatles and their families
> 'biographies', then no one
> would care. Obviously, for this to be interesting, she has to make it
> about 90% Beatles/John
> related, otherwise, no one would buy it.

1. Her relationship with John spanned much more than two years. I
doubt that she'll be concentrating on the Beatle years.

2. You *might* be right about much of her life story not being
*popular*, but I for one might find it interesting, depending on how
well it's written.

My guess is that there will be a significant portion devoted to Yoko's
childhood, upbringing, entrance into the art world, etc. -- i.e., the
"pre-John years". About 20% of the book, I'd guess.

Then about 15% of the book will cover the Beatles years. Then about 35%
on her years with John after the Beatles' breakup. Finally, about 30%
on the post-John years. That's my guess. We'll see how it works out.

What you guys are missing is that (whether it's actually true or
not)Yoko believes that her own accomplishments are *important*. To
believe that she would downplay that simply so she could capitalize on
her relationship with John is, in my view, naive.


RichL

1/6/2010 12:11:00 AM

0

abe slaney <abeslaney@yahoo.com> wrote:
> On Jan 5, 4:36 pm, Nutella <amongthebeliev...@yahoo.com> wrote:
> Obviously, for this to be interesting, she has to make it
>> about 90% Beatles/John
>> related, otherwise, no one would buy it.
>
> As has already been pointed out, that situation is no different than
> any other Beatles' spouse. Why the different standard for her? She was
> Lennon's intimate companion through the last years of the Beatles and
> into his solo career. This is not book-worthy?

I think the other Beatle spouses would be much more likely to "write"
something that was 90% about the Beatles.


poisoned rose

1/6/2010 12:35:00 AM

0

"RichL" <rpleavitt@yahoo.com> wrote:

> My guess is that there will be a significant portion devoted to Yoko's
> childhood, upbringing, entrance into the art world, etc. -- i.e., the
> "pre-John years". About 20% of the book, I'd guess.

Two certainties:

If the book has lots of Beatles material, the YTs will sneer about Yoko
feeding on the Beatles' fame to make money.

If the book has a lesser dose of Beatles material and instead
concentrates on herself, the YTs will sneer about Yoko overestimating
her importance.

Either way, there won't be a dry Depends in the YT camp.

RichL

1/6/2010 3:42:00 AM

0

Nutella <amongthebelievers@yahoo.com> wrote:
> I didn't realize this was such a "Pro-Ono" newsgroup. I'm surprised,
> almost shocked, to see a few people coming to her defense. I was
> always under the (possibly false) impression that die-hard Beatles
> fans didn't give Yoko much credibility, and that she was a poison to
> the future success of the band.

Once you're here for a while, you'll see that so many people come to her
defense simply because so many others consider her an easy target.
There's not much that's truly "pro", really. Just a (sometimes futile)
attempt to provide some balance.

Personally, I think she's a bit odd, I can't stand most of her singing,
I'm not into avant-garde art too much. But she's a human being and she
doesn't deserve the crap that's dished out to her here.

If she actually was a poison to the future success of the band (which is
debatable itself), it was John's choice to inflict her on the rest of
the group. Somehow, he escapes the heat for that, however.


RichL

1/6/2010 5:08:00 AM

0

Fattuchus <fattuchus@yahoo.com> wrote:
> On Jan 5, 11:32 pm, abe slaney <abesla...@yahoo.com> wrote:
>> On Jan 5, 11:19 pm, Fattuchus <fattuc...@yahoo.com> wrote:
>>
>>> She can write about her wonderful life (yawn) and hardly mention the
>>> Beatles or John.
>>
>> (yawn) yourself.
>
> See, Nutella, this is what I was talking about.

Typical Fatts. Use the yawn bit FIRST, then whine when someone else
turns it around on her. Then kiss up to the newb (who she's not even
responding to) in the process. Classic.