0

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.

user273072545345
  • 1,536
  • 2
  • 27
  • 57
  • @Santosh, just ran `reload!` right now, and then ran `LaLaLa.name_email` method again, and it still shows the same results in console – user273072545345 Feb 13 '14 at 05:38
  • You are clearly struggling with the ruby basics: just an half hour on http://tryruby.org will let you learn these by example. – nathanvda Feb 13 '14 at 11:13

2 Answers2

2

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
Ruby Racer
  • 5,690
  • 1
  • 26
  • 43
0

You should be setting the name like this

LaLaLa.first_name = 'Jesse'
LaLaLa.last_name = 'Smith'
LaLaLa.email = 'FakeEmail'
Santhosh
  • 28,097
  • 9
  • 82
  • 87