Dylan Karr
Dylan Karr

Reputation: 3564

Can this be DRYed up?

I was wondering if there's a good way to DRY this up.

if session[:working_post].nil?
  @post = Post.new
else
  @post = Post.new(session[:working_post])
  session[:working_post] = nil
end

Maybe something like

@post = Post.new(||= session[:working_post])

Is there anything like that?

Upvotes: 0

Views: 76

Answers (2)

Daniel Schmidt
Daniel Schmidt

Reputation: 11921

If session[:working_post] is a hash you could do this:

@post = Post.new(session[:working_post] || {})

Upvotes: 1

richjhall18
richjhall18

Reputation: 125

Would this work?

@post = Post.new(session.delete(:working_post) || {})

Upvotes: 5

Related Questions