Hi, I have a silly question. I have a form that is filled out that has a number of boolean values in it, represented by checkboxes. After the request is posted, I am generating an e-mail to send to the person handling the request. In my mailer view (send_request.text.plain.rhtml), I do something like Important thing one..: <%= @req.important_thing_1 %> Important thing two..: <%= @req.important_thing_2 %> Important thing three: <%= @req.important_thing_3 %> Which is rendered as Important thing one..: false Important thing two..: true Important thing three: false I would like false to display as ''No'' and true to display as ''Yes''. Is there a knob or switch somewhere in rails to configure how boolean values are converted into strings? Thank you. Regards, Rich --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Apr-02 19:33 UTC
Re: boolean? Emit "Yes" or "No", not "true" or "false"
Duzenbury, Rich wrote:> Hi, > > I have a silly question. I have a form that is filled out that has a > number of boolean values in it, represented by checkboxes. After the > request is posted, I am generating an e-mail to send to the person > handling the request. > > In my mailer view (send_request.text.plain.rhtml), I do something like > > Important thing one..: <%= @req.important_thing_1 %> > Important thing two..: <%= @req.important_thing_2 %> > Important thing three: <%= @req.important_thing_3 %> > > Which is rendered as > > Important thing one..: false > Important thing two..: true > Important thing three: false > > I would like false to display as ''No'' and true to display as ''Yes''. Is > there a knob or switch somewhere in rails to configure how boolean > values are converted into strings? > > Thank you. > > Regards, > > RichI understand you want to change boolean to yes or no depend on true or false :) If you want to use it in the view, you can put the code in the helper so you can access to it across all your template files :) def to_yesorno(boolean) if boolean == ''yes'' return ''yes'' else return ''no'' end end then you call on it puts to_yesorno(''true'') #should return yes :D -- 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 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2007-Apr-02 19:41 UTC
Re: boolean? Emit "Yes" or "No", not "true" or "false"
> I have a silly question. I have a form that is filled out that has a > number of boolean values in it, represented by checkboxes. After the > request is posted, I am generating an e-mail to send to the person > handling the request. > > In my mailer view (send_request.text.plain.rhtml), I do something like > > Important thing one..: <%= @req.important_thing_1 %> > Important thing two..: <%= @req.important_thing_2 %> > Important thing three: <%= @req.important_thing_3 %> > > Which is rendered as > > Important thing one..: false > Important thing two..: true > Important thing three: false > > I would like false to display as ''No'' and true to display as ''Yes''. Is > there a knob or switch somewhere in rails to configure how boolean > values are converted into strings?I don''t know if there is, but you could do something like this (and get it loaded via environment.rb): class TrueClass def to_yesno ''Yes'' end end class FalseClass def to_yesno ''No'' end end And then in your views do: Important thing one..: <%= @req.important_thing_1.to_yesno %> Important thing two..: <%= @req.important_thing_2.to_yesno %> Important thing three: <%= @req.important_thing_3.to_yesno %> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
kennethkunz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-03 02:16 UTC
Re: boolean? Emit "Yes" or "No", not "true" or "false"
try the ternary operator: bool ? ''Yes'' : ''No'' that will produce ''Yes'' for true and ''No'' for false; if you want an empty string for nil, try: (bool ? ''Yes'' : ''No'') unless bool.nil? that will return nil if bool is nil, which will turn into an empty string in an erb template (since nil.to_s is ''''). Of course, you could wrap that in a helper if you really want to, or add custom methods to TrueClass, FalseClass and NilClass... but personally, I find it clean/dry enough just to use the operator. Cheers, Ken --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Duzenbury, Rich
2007-Apr-03 13:53 UTC
Re: boolean? Emit "Yes" or "No", not "true" or "false"
> try the ternary operator: > > bool ? ''Yes'' : ''No'' > > that will produce ''Yes'' for true and ''No'' for false; if you > want an empty string for nil, try: > > (bool ? ''Yes'' : ''No'') unless bool.nil? > > that will return nil if bool is nil, which will turn into an > empty string in an erb template (since nil.to_s is ''''). > > Of course, you could wrap that in a helper if you really want > to, or add custom methods to TrueClass, FalseClass and > NilClass... but personally, I find it clean/dry enough just > to use the operator. >Hello, Thanks, that works fine. I normally avoid the ternary operator, but in this case, it''s a very straight forward replacement so I''ve chosen this route. Thanks to everyone who took the time to reply. Regards, Rich --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Just because I was curious: class TrueClass def to_s "Yes" end end class FalseClass def to_s "No" end end true.to_s => "Yes" false.to_s => "No" But this would mean *anywhere* you print out a boolean value it would render "Yes" or "No". -Shawn On 4/3/07, Duzenbury, Rich <RDuz-lc0hzAU4LMUAvxtiuMwx3w@public.gmane.org> wrote:> > > > try the ternary operator: > > > > bool ? ''Yes'' : ''No'' > > > > that will produce ''Yes'' for true and ''No'' for false; if you > > want an empty string for nil, try: > > > > (bool ? ''Yes'' : ''No'') unless bool.nil? > > > > that will return nil if bool is nil, which will turn into an > > empty string in an erb template (since nil.to_s is ''''). > > > > Of course, you could wrap that in a helper if you really want > > to, or add custom methods to TrueClass, FalseClass and > > NilClass... but personally, I find it clean/dry enough just > > to use the operator. > > > Hello, > > Thanks, that works fine. I normally avoid the ternary operator, but in > this case, it''s a very straight forward replacement so I''ve chosen this > route. > > Thanks to everyone who took the time to reply. > > Regards, > Rich > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Woops, didn''t realize that someone answered this already! Ignore me :) -Shawn On 4/3/07, Shawn Roske <sroske-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Just because I was curious: > > class TrueClass > def to_s > "Yes" > end > end > > class FalseClass > def to_s > "No" > end > end > > true.to_s => "Yes" > false.to_s => "No" > > But this would mean *anywhere* you print out a boolean value it would > render "Yes" or "No". > > -Shawn > > On 4/3/07, Duzenbury, Rich <RDuz-lc0hzAU4LMUAvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > try the ternary operator: > > > > > > bool ? ''Yes'' : ''No'' > > > > > > that will produce ''Yes'' for true and ''No'' for false; if you > > > want an empty string for nil, try: > > > > > > (bool ? ''Yes'' : ''No'') unless bool.nil? > > > > > > that will return nil if bool is nil, which will turn into an > > > empty string in an erb template (since nil.to_s is ''''). > > > > > > Of course, you could wrap that in a helper if you really want > > > to, or add custom methods to TrueClass, FalseClass and > > > NilClass... but personally, I find it clean/dry enough just > > > to use the operator. > > > > > Hello, > > > > Thanks, that works fine. I normally avoid the ternary operator, but in > > this case, it''s a very straight forward replacement so I''ve chosen this > > route. > > > > Thanks to everyone who took the time to reply. > > > > Regards, > > Rich > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Peter Ehrlich
2013-Feb-15 21:29 UTC
Re: boolean? Emit "Yes" or "No", not "true" or "false"
Found via google. I made a quick script for this. Sorry for reviving.. class TrueClass def to_s(style = :boolean) case style when :word then ''yes'' when :Word then ''Yes'' when :number then ''1'' else ''true'' end end end class FalseClass def to_s(style = :boolean) case style when :word then ''no'' when :Word then ''No'' when :number then ''0'' else ''false'' end end end https://gist.github.com/pehrlich/4963672 -- 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.