[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:03845] Re: Array.uniq! returning nil

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-07-05 17:42:23 UTC
List: ruby-talk #3845
NaHi:
> |Aleksi, how do you think 'String#gsub?'.  Seeing 'String#gsub?',
> |how do people think?

I have to think about those before I answer.

Matz:
> I don't vote for them, if they actually substitute strings.
> Predicates should not have any side effect.

I agree on this. But it's all about definition.

Let me define predicate to be a method which matches /[\w_]+\?/.

If we now say:

  array = [1,2,3]
  unless array.uniq!
    puts "no duplicates"
  end

But wait a second? Don't we feed if/unless with predicates like true, false,
nil? Then we must have a mistake here. *reading docs* No there's no mistake,
uniq! can be used as a "predicate" because it returns nil or the (now
modified) array which evaluates to non-nil, thus true.

So array.uniq! *is already* a "predicate" with a side effect.

Now, to remove '"' from my last sentence we could define predicate to match 
  /[\w_!]+\?/

Then we can mark our predicate capable to cause a side effect. When you see
array.uniq!? you know it modifies in place and acts as a predicate.

	- Aleksi

In This Thread

Prev Next