I’m trying to learn both Ruby and Rails and I’m looking at Michael Hartl’s RailsTutorial.org. In Chapter 7 or 8, he’s working on sessions and I think he’s using a method call as an argument to a function, but I’m not totally sure of it. It’s not something I’ve seen before.
Please have a look at the first function in the “private” section below where the argument is remember_token. does this mean that the return value of the function remember_token becomes the argument(s) for User.authenticate_with_salt?
def current_user
@current_user ||= user_from_remember_token
end
def signed_in?
!current_user.nil?
end
private
def user_from_remember_token
User.authenticate_with_salt(*remember_token)
end
def remember_token
cookies.signed[:remember_token] || [nil, nil]
end
Correct.
It’s also “splatted” (the
*), meaning the return value (the cookie bits, or the[nil, nil]array if there’s no cookie) is unpacked from the array, and sent as two individual parameters (roughly) toUser.authenticate_with_salt.