Hi, is there a ruby / rails builtin function that converts - 0 / 1 - "0" / "1" - "yes" / "no" - "true" / "false" to a boolean? cheers peter
On Jun 24, 2006, at 11:41, Pete wrote:> is there a ruby / rails builtin function that converts > > - 0 / 1 > - "0" / "1" > - "yes" / "no" > - "true" / "false" > > to a boolean?The answer is that they are evaluated as booleans when needed: if 0 puts "0 is true" end You probably knew that. If for whatever reason you need a mapping that returns false for 0, "0", "no", "false", you need to write your own. I guess the question has a valid justification so I won''t warn against that a priori :-). -- fxn
I would like this functionality to convert some user input into a boolean value for storing in the model. The user should be able to input different presentations of true and false and the method should return true / false I could imagine there is some method deep inside ActiveRecord / ActionPack / ActionSupport that does this already... example: "yes", "1", "t", "TRUE", ... -> true "No", "0", "000, "f", ", false", "FalSe", ... -> false Xavier Noria schrieb:> On Jun 24, 2006, at 11:41, Pete wrote: > >> is there a ruby / rails builtin function that converts >> >> - 0 / 1 >> - "0" / "1" >> - "yes" / "no" >> - "true" / "false" >> >> to a boolean? > > The answer is that they are evaluated as booleans when needed: > > if 0 > puts "0 is true" > end > > You probably knew that. If for whatever reason you need a mapping that > returns false for 0, "0", "no", "false", you need to write your own. I > guess the question has a valid justification so I won''t warn against > that a priori :-). > > -- fxn > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Saturday, June 24, 2006, at 11:41 AM, Pete wrote:>Hi, > >is there a ruby / rails builtin function that converts > >- 0 / 1 >- "0" / "1" >- "yes" / "no" >- "true" / "false" > >to a boolean? > >cheers >peter > > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsWhat do you want it to return if it doesn''t match either true or false? Keep in mind that ''false'' and ''nil'' both are treated as ''false'' in conditional tests by Ruby. _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
You could use a regular expression like: myfield.match(/(true|t|yes|y|1)$/i) != nil This will return true for case insensitive true, t, yes, y and 1 - false for everything else. Julian -- Posted via http://www.ruby-forum.com/.
That''s pretty neat... :-) Thanks! Julian Gall schrieb:> You could use a regular expression like: > > myfield.match(/(true|t|yes|y|1)$/i) != nil > > This will return true for case insensitive true, t, yes, y and 1 - false > for everything else. > > Julian > >
In rails 2, you can use ActiveRecord::ConnectionAdapters::Column.value_to_boolean("0") for 1,0,''true'',''false'',''1'',''0'', ''t'', and ''f''. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.