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??
Thanks...
Dave
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
try
[:subject_id, :book_id, :chapter_id, :section_id, :subsection_id,
:minisection_id].each do |k|
"@#{k.to_s}" = session[k] if session[k].present?
end
2013/2/17 Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
> 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??
>
> Thanks...
>
> Dave
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
att,
Rogerio
A complicação se descomplica na mesma proporção que fazemos os nós se
desatarem ao tecer o conhecimento do saber.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
or @subject_id = session[:subject_id] if session[:subject_id].present? @book_id = session[:book_id] if session[:book_id].present? @chapter_id = session[:chapter_id] if session[:chapter_id].present? @section_id = session[:section_id] if session[:section_id].present? @subsection_id = session[:subsection_id] if session[:subsection_id].present? @minisection_id = session[:minisection_id] if session[:minisection_id].present? 2013/2/17 Rogerio Medeiros <argerim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> try > > [:subject_id, :book_id, :chapter_id, :section_id, :subsection_id, > :minisection_id].each do |k| > "@#{k.to_s}" = session[k] if session[k].present? > end > > 2013/2/17 Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > >> 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?? >> >> Thanks... >> >> Dave >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > > -- > att, > > Rogerio > > A complicação se descomplica na mesma proporção que fazemos os nós se > desatarem ao tecer o conhecimento do saber. >-- att, Rogerio A complicação se descomplica na mesma proporção que fazemos os nós se desatarem ao tecer o conhecimento do saber. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Sun, Feb 17, 2013 at 7:16 PM, Rogerio Medeiros <argerim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> [:subject_id, :book_id, :chapter_id, :section_id, :subsection_id, > :minisection_id].each do |k| > "@#{k.to_s}" = session[k] if session[k].present?Sorry -- really not sure what''s supposed to be happening here -- you''re assigning to a string literal? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Feb 18, 2013 at 12:02 PM, tamouse mailing lists < tamouse.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Feb 17, 2013 at 7:16 PM, Rogerio Medeiros <argerim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > [:subject_id, :book_id, :chapter_id, :section_id, :subsection_id, > > :minisection_id].each do |k| > > "@#{k.to_s}" = session[k] if session[k].present? > > Sorry -- really not sure what''s supposed to be happening here -- > you''re assigning to a string literal? > > -- >apparently, he meant... instance_variable_set("@#{k.to_s}", session[k]) if session[k].present? kind regards -botp -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
botp wrote in post #1097518:> On Mon, Feb 18, 2013 at 12:02 PM, tamouse mailing lists < > tamouse.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> -- >> > > apparently, he meant... > > instance_variable_set("@#{k.to_s}", session[k]) if session[k].present? > > kind regards -botpThank you.. this different approach taught me alot. Dave -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
def set_instance_var_with_session_value variable
instance_variable_set("@#{variable.to_s}", session[variable]) if
session[variable].present?
end
and you will have a general method for this, for any symbol.
for example call set_instance_var_with_session_value(:subject_id)
it should work!
On Monday, 18 February 2013 00:43:42 UTC+1, Ruby-Forum.com User
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??
>
> Thanks...
>
> Dave
>
> --
> Posted via http://www.ruby-forum.com/.
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/EGTbOQ6YnjsJ.
For more options, visit https://groups.google.com/groups/opt_out.