Question

Ok, simplistic question.

There's this method in User model:

def name_email
   "#{first_name} #{last_name} - #{email}"
 end

All right, by the virtue of the fact that it doesn't have self attached to the method, one can deduct that it's an instance method, right?

So, I crack open the console, and try to initialize it like this:

 LaLaLa = User.new

Then I try to set this method by setting the first name first like this:

Jesse = LaLaLa.first_name

which gets me this:

=> nil

Then I try to set the last name:

Smith = LaLaLa.last_name

which gets me this again:

=> nil

Then I try to set email:

FakeEmail = LaLaLa.email 

which gets me this:

=> ""

Then I try to have the first name, last name and email by calling the method like this:

LaLaLa.name_email

which gets me this:

=> "  - "

Which brings me to my question, why is this not working in the console? And what am I doing wrong here?

I mean, I set the first name, last name, and email as you can see.

Why doesn't the method display the results?

Let me know if this question could be phrased better.

Était-ce utile?

La solution 2

You should be setting the name like this

LaLaLa.first_name = 'Jesse'
LaLaLa.last_name = 'Smith'
LaLaLa.email = 'FakeEmail'

Autres conseils

Have you seen what you have written?

You are getting nil because you are giving a nil value to an unset variable...

Jesse is an unset variable, and you are giving it the value at LaLaLa.first_name...

You should do

LaLaLa.first_name = "Jesse"

etc...

and at the end

LaLaLa.save

Note: In ruby it's common practice to give instance variables names with the first letter non capital. Capital first letters mean classes... So, to be correct, do :

lalala=User.new
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top