On Mon, Feb 18, 2013 at 12:42 AM, Dave Castellano <[email protected]> wrote:
> Hi,
>
> As I continue learning to program, I am finding things I think can
> probably be
> done in a better way. The following code seems like something
> programmers must run into all the time and I am wondering if there is a
> better way to write the code in this situation (it seems very repetitive
> to me). I'm having fun and trying to get better at this....
>
> if !session[:subject_id].blank?
> @subject_id = session[:subject_id]
> end
>
> if !session[:book_id].blank?
> @book_id = session[:book_id]
> end
>
> if !session[:chapter_id].blank?
> @chapter_id = session[:chapter_id]
> end
>
> if !session[:section_id].blank?
> @section_id = session[:section_id]
> end
>
> if !session[:subsection_id].blank?
> @subsection_id = session[:subsection_id]
> end
>
> if !session[:minisection_id].blank?
> @minisection_id = session[:minisection_id]
> end
>
> Is there a better way??
We do not know where "session" comes from. But it might be an option
to just store session in a member variable and reference values there.
Or you use a generic copying mechanism, e.g.
@session_data ||= {}
session.keys.each {|k| val = session[k] and @session_data[k] = val}
There may be even more solutions. We would have to know more about
the code to come up with more targeted ideas.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
--
[email protected] |
https://groups.google.com/d/forum/ruby-talk-google?hl=en
---
You received this message because you are subscribed to the Google Groups
"ruby-talk-google" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.