-1

I would like to know what the difference is between these two examples:

my_name = gets.chomp
my_name.capitalize 

and

my_name = gets.chomp
my_name.capitalize!
sawa
  • 165,429
  • 45
  • 277
  • 381
  • Read through the Ruby documentation, especially the class documentation for the methods that come with Ruby. They are your friends. [`capitalize`](http://ruby-doc.org/core-2.3.1/String.html#method-i-capitalize) vs. [`capitalize!`](http://ruby-doc.org/core-2.3.1/String.html#method-i-capitalize-21) It's really important, if you're going to program, that you get in the habit of reading the documentation of any language you pick up. Tutorials are OK, but the documentation is the source of truth. – the Tin Man Jul 06 '16 at 20:39

3 Answers3

3

The difference is

my_name.capitalize

returns a capitalized version of my_name without affecting the object my_name points to, while

my_name.capitalize!

still returns a capitalized version of my_name but my_name is changed too, so

my_name = "john"
puts my_name.capitalize # print 'John' but the value of my_name is 'john'
puts my_name.capitalize! # print 'John' and now the value of my_name is 'John'
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ursus
  • 29,643
  • 3
  • 33
  • 50
  • I don't understand what you mean by "returns a capitalized version of my_name without affecting the object that my_name points to" – Yasmine Guemouria Jul 06 '16 at 14:06
  • You know that a variable keeps a value. In the first case, capitalize, is a method that returns a string with first letter capitalized and don't change the value of your variable. In the latter, the method returns a string with the first letter capitalized too but it changes the value of your variable too. – Ursus Jul 06 '16 at 14:12
  • thank you, i do understand now! – Yasmine Guemouria Jul 06 '16 at 14:46
  • Generally speaking, one must not rely on return value of in-place-mutation methods. They return nil sometimes. Their primary purpose is the side effect. – Sergio Tulentsev Jul 06 '16 at 16:06
1

From the Ruby capitalize docs:

capitalize

Returns a copy of str with the first character converted to uppercase and the remainder to lowercase.

capitalize!

Modifies str by converting the first character to uppercase and the remainder to lowercase. Returns nil if no changes are made.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Charizard_
  • 1,215
  • 1
  • 9
  • 20
0

I'm always so happy to see somebody getting into ruby!

The thing with ruby is that, even though it's a very friendly language, it assumes a lot of things without necessarily telling the newbies about it. They make lots of sense once you have a couple months under the belt with the language, but not before, so I understand your question.

First of all, the bang (!) is just part of the name itself. Ruby allows exclamation points and question marks as part of the method name just as any other character. Cool, right?

Why do people bother, though? Well, it's a convention. As a rule of thumb, an accepted explanation of why a method should have a bang sign is that the method does an invasive, destructive or mutating thing, that is, it destroys data, runs a transaction on the database, permanently changes data, etc.

It's not obligatory to name these kinds of methods like this, but it's a convention that's very well withheld in the Ruby community.

Programming Ruby says:

Methods that are "dangerous," or modify the receiver, might be named with a trailing "!".

Hope this answers your question.

Alfredo Gallegos
  • 613
  • 6
  • 22