Jason Vogel
2006-Dec-06 19:51 UTC
Clean way to "inject" raw text from within a <%%> block?
Source : <% case list_type.upcase when "BASE" then %> Base Coverages <% when "OPTIONAL" then %> Optional Coverages <% else "asdfadasdF" %> Bogus <% end %> The above just looks sloppy to me... I would have hoped for something like... <% case list_type.upcase when "BASE" then inject("Base Coverages") when "OPTIONAL" then inject("Optional Coverages") else "asdfadasdF" then inject("Bogus") end %> I hate matching up the <%... ''s. It is just to prone to error. Jason --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Francois Beausoleil
2006-Dec-06 20:08 UTC
Re: Clean way to "inject" raw text from within a <%%> block?
Hello Jason, 2006/12/6, Jason Vogel <jasonvogel@gmail.com>:> <% case list_type.upcase > when "BASE" then %> Base Coverages > <% when "OPTIONAL" then %> Optional Coverages > <% else "asdfadasdF" %> Bogus > <% end %>case returns it's value, so you can do it like this: <%= case list_type.upcase when 'BASE'; "Base Coverages" when 'OPTIONAL'; "Optional Coverages" else; "whatever end %> Hope that helps ! -- François Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jean-François
2006-Dec-06 20:13 UTC
Re: Clean way to "inject" raw text from within a <%%> block?
Hello Jason,> Source : > <% case list_type.upcase > when "BASE" then %> Base Coverages > <% when "OPTIONAL" then %> Optional Coverages > <% else "asdfadasdF" %> Bogus > <% end %> > > The above just looks sloppy to me... > > I would have hoped for something like... > > <% case list_type.upcase > when "BASE" then inject("Base Coverages") > when "OPTIONAL" then inject("Optional Coverages") > else "asdfadasdF" then inject("Bogus") > end %>you're not far : <%= case list_type.upcase when 'BASE' : 'Base Coverages' # different style when "OPTIONAL" then "Optional Coverages" else "asdfadasdF" then "Bogus" end %> You can also use an Hash with "Bogus" as default value. -- Jean-François. -- À la renverse. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Vogel
2006-Dec-06 21:03 UTC
Re: Clean way to "inject" raw text from within a <%%> block?
Both of these are great. Is there a general technique to this? If I had done with an "If..Elsif..." block, what would it have looked like? And is there a way to "inject" text? Thanks, Jason On Dec 6, 2:13 pm, "Jean-François" <jf.w...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello Jason, > > > > > Source : > > <% case list_type.upcase > > when "BASE" then %> Base Coverages > > <% when "OPTIONAL" then %> Optional Coverages > > <% else "asdfadasdF" %> Bogus > > <% end %> > > > The above just looks sloppy to me... > > > I would have hoped for something like... > > > <% case list_type.upcase > > when "BASE" then inject("Base Coverages") > > when "OPTIONAL" then inject("Optional Coverages") > > else "asdfadasdF" then inject("Bogus") > > end %>you''re not far : > > <%= case list_type.upcase > when ''BASE'' : ''Base Coverages'' # different style > when "OPTIONAL" then "Optional Coverages" > else "asdfadasdF" then "Bogus" > end %> > > You can also use an Hash with "Bogus" as default value. > > -- Jean-François. > > -- > À la renverse.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jean-François
2006-Dec-06 21:29 UTC
Re: Clean way to "inject" raw text from within a <%%> block?
Jason :> Both of these are great. Is there a general technique to this? If I > had done with an "If..Elsif..." block, what would it have looked like?<%= if list_type.upcase =='BASE' 'Base Coverages' else 'Bogus' %> Or using ? : <%= list_type.upcase == 'BASE' ? 'Base Coverages' : 'Bogus' %> Note that in Ruby code, you can do assignments like that : foo = if something == something_else 'bar' else 'baz' end but ? : will be more often used. And you can use case/when as well foo = case .. when ... when ... end> And is there a way to "inject" text?yes : _erbout.concat('foo') don't forget also, that helpers can make your ERb code cleaner. -- Jean-François. -- À la renverse. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---