3

I'm reading a guide about Rails and I came across this line in a Model class:

before_save { |user| user.email = email.downcase }

This is to make sure that the email address is lowercase before it hits the database (but you already know that, because you guys and gals are smart!).

However, why not just have this:

before_save { |user| user.email.downcase! }

Wouldn't that be simpler to execute, or am I missing something?

RileyE
  • 10,874
  • 13
  • 63
  • 106
  • @KMcA Using the Michael Hartl guide? – RileyE May 22 '13 at 18:12
  • Yes. Really enjoying it so far. – KMcA May 22 '13 at 18:19
  • @KMcA Good! I did, too. Make sure to save it. It will be a big help and make a lot more sense down the road. [Rails for Zombies](http://railsforzombies.org/) is also a good resource. – RileyE May 22 '13 at 18:20
  • Yes, I've done Zombies1 and am doing Zombies2 in a free demo they have this weekend. Thanks for the advice. I actually had already planned on reading it again. Best web book I've read ever, but I've only been at this 6 months so that may not be too fair of a comparison. – KMcA May 22 '13 at 18:25
  • @KMcA It's been the best one I've found in my couple years of development on many different platforms. Since you found this question, you seem to be thinking the right way. Keep it up and don't feel hesitant to [ask questions, even if you think you're being stupid](http://stackoverflow.com/questions/12631665/fibonacci-sequence-in-javascript). Those are the easiest to solve and best for other new developers! :) – RileyE May 22 '13 at 18:28

1 Answers1

2

Both do the same.. it just comes down to taste.

Tigraine
  • 23,358
  • 11
  • 65
  • 110
  • Neither is more optimal? I thought the one would have to access a second spot in the memory, thus being slower. – RileyE Nov 02 '12 at 00:07
  • That's really hard to say as it depends largely on how the Ruby VM you are running on is implemented.. Keep in mind that there are VMs that have Immutable Strings like the JavaVM (at least I think it is) so any string operation like the above will have to copy around the string.. – Tigraine Nov 02 '12 at 00:35
  • Also I would not worry about these kinds of optimizations.. There are other things to worry about that will cost you far more perf (like N+1s or other easy to make mistakes) than if a string gets copied around or not :) – Tigraine Nov 02 '12 at 00:38
  • Okay. Thank you for that clarification. I was actually under the assumption that all String objects in Ruby were mutable. – RileyE Nov 02 '12 at 00:42
  • It hugely depends on what VM you are running.. It may be the case in MRI but is absolutely not in JRuby. – Tigraine Nov 02 '12 at 00:50
  • I'm too new to figure out about RVM. I tried googling RVM and a bunch of things about strings to see if it was mutable, but no such luck. Do you know? Its the VM I'll probably stick with, at least for a while. Also, if you enjoy the points, I've posted a new question that you can probably knock out pretty easily. – RileyE Nov 02 '12 at 14:19
  • VM != RVM .. RVM is the Ruby version manager while with VM i meant the Ruby Virtual Machine. As Ruby is not compiled but rather interpreted these optimizations depend on the interpretor and the interpretor's operating environment. And jRuby for example runs inside the Java Virtual Machine so it's strings are Immutable. – Tigraine Nov 04 '12 at 23:43
  • Oh. I'm sorry. I wasn't thinking very clearly (clearly!). I'm pushing to Heroku, so would the VM be hosted there, or is it something I'm uploading myself? – RileyE Nov 05 '12 at 15:43
  • Heroku is providing you with the VM.. and Heroku is running MRI Ruby 1.9.3.. no idea how strings are handled in MRI .. – Tigraine Nov 05 '12 at 20:39