3

Why is the power! method in Ruby's Fixnum class named with an exclamation mark? By convention, methods that have a name ending in an ! are potentially dangerous (for example, they may modify the instance in some way).

I can appreciate the difference between gsub and gsub! in String, but what's up with power!? It doesn't seem to modify the Fixnum instance or perform any other "dangerous" actions; it just returns the result.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Jeff
  • 21,744
  • 6
  • 51
  • 55
  • Bang methods mean danger, not necessarily modification. See http://stackoverflow.com/questions/709229/difference-between-downcase-and-downcase-in-ruby (I'll remove the downvote once this is fixed) – Andrew Grimm Dec 29 '09 at 11:21
  • The Ruby web site (ruby-lang.org) links to a tutorial that states specifically that bang methods modify the receiver: http://rubylearning.com/satishtalim/writing_own_ruby_methods.html. "Ruby methods that modify an object in-place and end in an exclamation mark are known as bang methods." So... who's right? – Jeff Dec 29 '09 at 19:08
  • Ouch. However, it later says "Normally for the built-in classes, dangerous usually (although not always) means this method, unlike its non-bang equivalent, permanently modifies its receiver.". I'll reverse my downvote if you provide a blank edit to the question (I can't undo my downvote otherwise) – Andrew Grimm Dec 30 '09 at 05:25
  • I took the middle road, since I have no clue who is right. I guess it's a convention where "danger" could be anything. Yet, in the built-in classes, it's only thus far been used in cases of receiver modification. – Jeff Dec 30 '09 at 06:19

1 Answers1

2

It's just an inconsistency in the API. Nothing to worry about, move along :)

The ! is just a convention for destructive methods, not a rule.

rfunduk
  • 30,053
  • 5
  • 59
  • 54