Whaaaaat?

T

Tom Cloyd

p [0..5].include? 0
false
p [0..5].member? 0
false

This is nuts. The world is flat, evidently.

Can someone explain this nonsense?

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
T

Tom Cloyd

Tom said:
p [0..5].include? 0
false
p [0..5].member? 0
false

This is nuts. The world is flat, evidently.

Can someone explain this nonsense?

t.
Sorry - inadequate details: running ruby 1.8.7 (2008-08-11 patchlevel
72) [i486-linux]

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
B

Ben Bleything

p [0..5].include? 0
false
p [0..5].member? 0
false

This is nuts. The world is flat, evidently.
Nope.

Can someone explain this nonsense?

Yep. You've got an array with a single member... the range 0..5.

I think what you meant is:

(0..5).include? 0
=> true

:)

Ben
 
K

Kirk Haines

p [0..5].include? 0
false
p [0..5].member? 0
false

This is nuts. The world is flat, evidently.

Can someone explain this nonsense?

[0..5] is an array with a single element, a Range object representing
the range from 0 to 5.

(0..5).to_a gives you [0,1,2,3,4,5] which is probably what you assumed
you got with [0..5]

(0..5).include?(0) will work, as well, because ranges support the
include? method.


Kirk Haines
 
T

Tom Cloyd

Ben said:
p [0..5].include? 0
false
p [0..5].member? 0
false

This is nuts. The world is flat, evidently.

Nope.


Can someone explain this nonsense?

Yep. You've got an array with a single member... the range 0..5.

I think what you meant is:

(0..5).include? 0
=> true

:)

Ben
Well, nuts.

This clarifies it a bit:

irb(main):009:0> (1..10).class
=> Range
irb(main):010:0> [1..10].class
=> Array
irb(main):011:0>

Why can't ruby just do what I mean, rather than what I say? Is that
asking too much? (heh heh)

Thanks for the clarifications. This list has not equal for speedy
answers (usually).

t.



--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
T

Tom Cloyd

Kirk said:
p [0..5].include? 0
false
p [0..5].member? 0
false

This is nuts. The world is flat, evidently.

Can someone explain this nonsense?

[0..5] is an array with a single element, a Range object representing
the range from 0 to 5.

(0..5).to_a gives you [0,1,2,3,4,5] which is probably what you assumed
you got with [0..5]

(0..5).include?(0) will work, as well, because ranges support the
include? method.


Kirk Haines
Thanks, Kirk. That's the best answer yet. Very helpful.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
R

Robert Dober

Why can't ruby just do what I mean, rather than what I say? Is that asking
too much? (heh heh)
But it does, I am sure you just forgot the *, right?
[*0..5].include? 42
--> false
Cheers
Robert
 
T

Tom Cloyd

Robert said:
Why can't ruby just do what I mean, rather than what I say? Is that asking
too much? (heh heh)
But it does, I am sure you just forgot the *, right?
[*0..5].include? 42
--> false
Cheers
Robert
Robert,

Interesting, but obscure. I just did a quick dig into Thomas (Pickaxe,
3rd ed.), to try to figure out what [*1..5] does, and I still don't get
it. Can you explain?

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
T

Todd Benson

Robert said:
Why can't ruby just do what I mean, rather than what I say? Is that
asking
too much? (heh heh)

But it does, I am sure you just forgot the *, right?
[*0..5].include? 42
--> false
Cheers
Robert

Robert,

Interesting, but obscure. I just did a quick dig into Thomas (Pickaxe, 3rd
ed.), to try to figure out what [*1..5] does, and I still don't get it. Can
you explain?

The"pre-appending" (yes I'm aware that's not a word or proper
conjuction) _star_ flattens arrays/ranges. Well, not exactly; it
allows you to use a "list" of things. Some people avoid it because of
a supposed performance issue. Pick your battle, I guess.

It happens to be useful to me. I really hope they don't get rid of
that nomenclature (much like getting rid of #inject; if so, goodbye
Ruby for me).

Todd
 
R

Robert Dober

Robert said:
Why can't ruby just do what I mean, rather than what I say? Is that
asking
too much? (heh heh)


But it does, I am sure you just forgot the *, right?
[*0..5].include? 42
--> false
Cheers
Robert

Robert,

Interesting, but obscure. I just did a quick dig into Thomas (Pickaxe, 3= rd
ed.), to try to figure out what [*1..5] does, and I still don't get it. = Can
you explain?

The"pre-appending" (yes I'm aware that's not a word or proper
conjuction) _star_ flattens arrays/ranges. =A0Well, not exactly; it
allows you to use a "list" of things. =A0Some people avoid it because of
a supposed performance issue. =A0Pick your battle, I guess.

It happens to be useful to me. =A0I really hope they don't get rid of
that nomenclature (much like getting rid of #inject; if so, goodbye
Ruby for me).
I do not believe I have ever heard of that intention, if they get rid
of *0..3 than there is nothing one can do, if they get rid of inject
we will just reimplement it ;)
Robert


--=20
Si tu veux construire un bateau ...
Ne rassemble pas des hommes pour aller chercher du bois, pr=E9parer des
outils, r=E9partir les t=E2ches, all=E9ger le travail=85 mais enseigne aux
gens la nostalgie de l=92infini de la mer.

If you want to build a ship, don=92t herd people together to collect
wood and don=92t assign them tasks and work, but rather teach them to
long for the endless immensity of the sea.
 
A

Adam Gardner

Chad said:
I'm pretty sure the word you want is either "prefixing" or, if you like
the hypercorrect neologism, "prepending".

Hrm. I had always thought of 'prepend' as a commonplace, dictionary
word, and was about to point out that it was such, until I accidentally
looked it. Neologismtastic!
 
T

Tom Cloyd

Adam said:
Hrm. I had always thought of 'prepend' as a commonplace, dictionary
word, and was about to point out that it was such, until I accidentally
looked it. Neologismtastic!
Actually, this is all unnecessary. In linguistics, for eons, we have
spoken of prefix and suffix usage, So, this is a prefix asterisk, as in
"...the meaning of this prefix asterisk is blah blah...". That says it
just fine, with no new language. Travel light.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
B

Brian Candler

Todd said:
It happens to be useful to me. I really hope they don't get rid of
that nomenclature (much like getting rid of #inject; if so, goodbye
Ruby for me).

I never heard any proposal to get rid of Enumberable#inject, and I don't
think it's something you need to worry about. Ruby 1.9 has been adding
hundreds of new methods, not taking them away.
 
J

Jeff Moore

Brian said:
I never heard any proposal to get rid of Enumberable#inject, and I don't
think it's something you need to worry about. Ruby 1.9 has been adding
hundreds of new methods, not taking them away.

1.9.1 adds Enumerable#reduce as synonym (which makes more sense to me)
and
still retains inject
 
R

Rick DeNatale

Is one of #reduce or #inject deprecated in 1.9.1?

Don't think so.

Actually IMHO which makes more sense is situational. To me reduce
makes sense when the optional initial argument is not used, and inject
makes more sense when it is:

collection.reduce {|memo, element| ...}
vs
collection.inject(initial_value) {|memo, element| ...}

Several of the Enumerable method names were pretty obviously inspired
by the Smalltalk collection hierarcy, select, detect, reject, and
inject among them. The Smalltalk method is inject:info: which makes
the meaning of inject a bit clearer, in Smalltalk you would write the
second example as:

collection inject: initialValue into: [memo, element| ...]

You are injecting the initialValue into the sequence given to the block.

And there are other instances of long-standing aliases like find for detect=
 
R

Robert Dober

Personally I think it's good to have such synonyms, they make it
easier to be express subtleties just as they do for authors in natural
languages.
In my humble opinion I believe that having different words meaning the
same thing is not bad. It enables a certain ease to adapt our programs
slightly accordingly to slightly different cases. This is exactly what
they do for people expressing themselves in human idioms.

I guess that was what you meant, though I apologize for a certain lack
of subtlety.
;)
R.
 
R

Rick DeNatale

Really? =A0I never heard of "inject" in any other language. =A0It was alw= ays
"reduce" to me. =A0I got the impression that "inject" was just a cutesy w= ay of
making the method name similar to detect, select, collect, and reject.

So did you stop reading my reply at that point?

And if you want to let languages vote on what the 'correct' name
should be maybe it should be fold:

http://en.wikipedia.org/wiki/Fold_(higher-order_function)#Examples

Fold gets 8 'votes',
Reduce gets 7, but that's counting ruby's afterthought, and Python
twice since it changed slightly between Python 2.x and 3.x so maybe
that should only be 5 'votes'
Inject gets 'votes' from Smalltalk and Ruby

Either reduce or inject is probably the earliest though. Probably
reduce since that's what Common Lisp uses, BUT CL is a tad newer than
Smalltalk, and I'm not sure where in Lisp's long evolution from Lisp
1.5 reduce came into common usage. I'm a bit perturbed that I can't
seem to find my copy of the Lisp 1.5 Programmers Manual which is
probably buried somewhere in my attic.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top