1

i was writing code to append new email to an empty array in my Rails App. like this:

@users_email = Array.new 
@users_email << User.find_by_id(@comment.user_id).email 
@users_email << User.find_by_id(Comment.find_by_id(@parent_id).user_id).email if !@parent_id.nil?

Note: Here @comment is just a hash of id, user_id, parent_id and @parent_id is a the id of a Parent of any Comment. as in Hierarchy of Parent-child.

But instead of having two items in array ex: ["[email protected]", "[email protected]"] I am getting only one item ex:["[email protected]"] after appending second item.

My confusion starts when I try to save above expression in an Instance variable and tries to append the same into an empty array, It behaves as expected. Ex:

@users_email = Array.new
@first = User.find_by_id(@comment.user_id).email
@second = User.find_by_id(Comment.find_by_id(@parent_id).user_id).email if !@parent_id.nil?

@users_email << @first  # ["[email protected]"]
@users_email << @second  # ["[email protected]", "[email protected]"]

where as this wasn't the case in previous one. Can someone please explain whats happening here.

Update

By mistake I have place "=" instead of "<<" while appending first element to the array for the first case and after appending second one I got the result something like "[email protected]@xyz.com" but for some reason in my Rails Application I was getting only the first email id in return.ex: ["[email protected]"]

So, probably this question is now not relevant to the issue raised. Sorry for bugging all of you.

7
  • In your last code block you've got @user_email = Array.new, @users_email << @first and @user_email << @second. Was that a typo in the @users_email << @first part? (there's an s in there, which isn't on the other two lines). Also, in Ruby you usually write [] over Array.new. So, instead of @user_email = Array.new you would have @user_email = []. Commented May 24, 2015 at 6:14
  • Ohh yeah I thats a typo Commented May 24, 2015 at 6:22
  • Ok, so was the content of @comment.user_id the same in both cases? Commented May 24, 2015 at 6:24
  • Yes its the same in both case..infact both the use case are having exact input just the change in writing code Commented May 24, 2015 at 6:26
  • are you sure you got ["[email protected]"] instead of ["[email protected]"] when you were expecting ["[email protected]", "[email protected]"]? Commented May 24, 2015 at 7:12

1 Answer 1

1

That's because you have this check !@parent_id.nil? at the end of the following statement:

 @users_email << User.find_by_id(Comment.find_by_id(@parent_id).user_id).email if !@parent_id.nil?

In the first case, @parent_id apparently is nil, so the statement never executes: hence only one value is appended to the array.

Aside: prefer using the array literal syntax (ary = [] over ary = Array.new)

Sign up to request clarification or add additional context in comments.

6 Comments

That line is also in his second code block. There are some typos in that code block though (as I mentioned in my comment on his post).
I realize that, but, like I said, in the first case, @parent_id must have been nil while in the second case it wasn't. I'm fairly certain the behavior you're seeing is just a coincidence since the behavior of the two blocks is nearly identical aside from the additional instance variables.
Yeah, I understand that, but he got ["[email protected]"] in his first try. That's from that second line, the line that you mentioned in your answer.
Also, using [] over Array.new doesn't affect the behavior if i am not wrong
No it doesn't. It's just poor style. Regarding your code, it must have been nil or @second had to have been defined before. It's impossible for the return of that line (User.find_by ... if !@parent_id.nil?) to differ otherwise.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.