Hello, Consider this code, it gets a mention and an array of tags. It finds the mentions and add the tags. But in my methods I keep using return if foo.nil? or execute a line of code if a line is not nil. Is there a better way of doing this kind of thing? Thks def add_tags_to_mention mention_id, *tags mention = Mention.find mention_id return if mention.nil? tags.each do |tag_id| tag = Tag.find tag_id mention.tags.push(tag.to_mongo) if tag end end -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Vre8k4twtb4J. For more options, visit https://groups.google.com/groups/opt_out.
On Tue, Nov 13, 2012 at 6:22 AM, rodrigo coutinho <rodrigo.coutinho15-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Consider this code, it gets a mention and an array of tags. > It finds the mentions and add the tags. > But in my methods I keep using return if foo.nil? > or execute a line of code if a line is not nil. > Is there a better way of doing this kind of thing? > Thks > > def add_tags_to_mention mention_id, *tags > mention = Mention.find mention_id > return if mention.nil? > tags.each do |tag_id| > tag = Tag.find tag_id > mention.tags.push(tag.to_mongo) if tag > end > endPersonally I would handle that a bit differently in that I would have the model handle building it''s own tags and I would inject an array [] and then delete_if value blank? but to address your concern directly: I would never return nil unless I have to. I know quite a few Ruby programmers do not care about that situation but I always return false, true or the value of the value I processed... there are a lot of cases where I will also to opt to just raise but that really depends... in your situation I would have built it on the model as Mention.build_and_save_tags and then had it raise if it could not find the mention because to me that makes the most sense. What I am saying is to me `return false if mention.blank?` makes more sense to me. I could go on a long explanation a mile long explaining the difference between situations and why I chose my flow to work those ways but I''ll just leave it at I would prefer false before nil. -- 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 https://groups.google.com/groups/opt_out.
Returning nil is a little bit ambiguous. Did the method finished correctly or a condition wasn''t met? I always try to return something or throw an exception when needed. I don''t know if there''s a "best practice" for that. On Tue 13 Nov 2012 10:24:00 AM ART, Jordon Bedwell wrote:> On Tue, Nov 13, 2012 at 6:22 AM, rodrigo coutinho > <rodrigo.coutinho15-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Consider this code, it gets a mention and an array of tags. >> It finds the mentions and add the tags. >> But in my methods I keep using return if foo.nil? >> or execute a line of code if a line is not nil. >> Is there a better way of doing this kind of thing? >> Thks >> >> def add_tags_to_mention mention_id, *tags >> mention = Mention.find mention_id >> return if mention.nil? >> tags.each do |tag_id| >> tag = Tag.find tag_id >> mention.tags.push(tag.to_mongo) if tag >> end >> end > > Personally I would handle that a bit differently in that I would have > the model handle building it''s own tags and I would inject an array [] > and then delete_if value blank? but to address your concern directly: > > I would never return nil unless I have to. I know quite a few Ruby > programmers do not care about that situation but I always return > false, true or the value of the value I processed... there are a lot > of cases where I will also to opt to just raise but that really > depends... in your situation I would have built it on the model as > Mention.build_and_save_tags and then had it raise if it could not find > the mention because to me that makes the most sense. What I am saying > is to me `return false if mention.blank?` makes more sense to me. I > could go on a long explanation a mile long explaining the difference > between situations and why I chose my flow to work those ways but I''ll > just leave it at I would prefer false before nil. >-- -- Mis mejores deseos, Best wishes, Meilleurs vœux, Juan Pablo ------------------------------------------------------ http://www.jpgenovese.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@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Alright, based on your opinion of returning nil,I changed the method a little bit. In fact return false is better. Thank you. class Mention def self.build_and_save_tags mention_id, *tags mention = Mention.find mention_id return false if mention.blank? tags.each do |tag_id| tag = Tag.find tag_id if tag mention.tags.push(tag.to_mongo) mention.save end end end end On Tuesday, November 13, 2012 11:25:48 AM UTC-2, Jordon Bedwell wrote:> > On Tue, Nov 13, 2012 at 6:22 AM, rodrigo coutinho > <rodrigo.c...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> wrote: > > Consider this code, it gets a mention and an array of tags. > > It finds the mentions and add the tags. > > But in my methods I keep using return if foo.nil? > > or execute a line of code if a line is not nil. > > Is there a better way of doing this kind of thing? > > Thks > > > > def add_tags_to_mention mention_id, *tags > > mention = Mention.find mention_id > > return if mention.nil? > > tags.each do |tag_id| > > tag = Tag.find tag_id > > mention.tags.push(tag.to_mongo) if tag > > end > > end > > Personally I would handle that a bit differently in that I would have > the model handle building it''s own tags and I would inject an array [] > and then delete_if value blank? but to address your concern directly: > > I would never return nil unless I have to. I know quite a few Ruby > programmers do not care about that situation but I always return > false, true or the value of the value I processed... there are a lot > of cases where I will also to opt to just raise but that really > depends... in your situation I would have built it on the model as > Mention.build_and_save_tags and then had it raise if it could not find > the mention because to me that makes the most sense. What I am saying > is to me `return false if mention.blank?` makes more sense to me. I > could go on a long explanation a mile long explaining the difference > between situations and why I chose my flow to work those ways but I''ll > just leave it at I would prefer false before nil. >-- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/MCLF0MG0HFwJ. For more options, visit https://groups.google.com/groups/opt_out.