tag:blogger.com,1999:blog-16002962.post4366976416407113128..comments2023-10-13T01:00:52.135-07:00Comments on Web Jazz: Using anonymous functions inside functionsWil Chttp://www.blogger.com/profile/03696320260631888445noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-16002962.post-70263801897436604972007-09-01T07:57:00.000-07:002007-09-01T07:57:00.000-07:00John: Your point about simplicity is well taken. W...John: Your point about simplicity is well taken. When one is dealing with actual working code, KISS is the acronym to remember. However, on a blog like Wilhelm's, code is often posted for exemplary purposes only, to illustrate an idea. Anonymous functions might be a better idea given more complicated circumstances, but who wants to post (or read) complicated code examples in a blog?Michael Harrisonhttps://www.blogger.com/profile/01532019950536814477noreply@blogger.comtag:blogger.com,1999:blog-16002962.post-17712106855611510292007-08-31T15:56:00.000-07:002007-08-31T15:56:00.000-07:00Hrm...John's comment gives me pause for thought. ...Hrm...John's comment gives me pause for thought. While I don't think using Proc is necessarily better or worthwhile in this case, it does put a different focus on the code. <BR/><BR/>Which way is better should be which one that is clearer. Unfortunately, I think Which one is clearer probably depends on what you're use to.<BR/><BR/>Recently, I've had a preference for functional style programming, and hence Michael's method looks perfectly ok to me. <BR/><BR/>Perhaps this is a case of using a functional style when it doesn't have a clear advantage over imperative style--other than a different focus for the code.Wil Chttps://www.blogger.com/profile/03696320260631888445noreply@blogger.comtag:blogger.com,1999:blog-16002962.post-7890292395798600082007-08-31T15:20:00.000-07:002007-08-31T15:20:00.000-07:00Looks like someone got a new hammer, and everythin...Looks like someone got a new hammer, and everything is looking like a nail.<BR/><BR/>I would have factored out the shared code like so:<BR/>def bookmark_button(page, user, indicator_id)<BR/> if page.bookmarked_by? session[:user]<BR/> "Bookmarked!"<BR/> else<BR/> link_text = " Bookmark!"<BR/> if page.done_by? session[:user]<BR/> link_text = " Bookmark again!"<BR/> end<BR/><BR/> link_to_remote image_tag('heart_add.png', :height => 16, :width => 16) + link_text, <BR/> :url => { :controller => :bookmark_list, :action => :add, :page_id => page.id }, <BR/> :loading => "Element.toggle('#{indicator_id}')", <BR/> :complete => "Element.toggle('#{indicator_id}')"<BR/> end<BR/>end<BR/><BR/><BR/>Am I missing something?Huffhttps://www.blogger.com/profile/17622652320471746903noreply@blogger.comtag:blogger.com,1999:blog-16002962.post-52319649656365085692007-08-31T10:37:00.000-07:002007-08-31T10:37:00.000-07:00Daddy likes...!Huh, I didn't think of just mashing...Daddy likes...!<BR/><BR/>Huh, I didn't think of just mashing it all together. I think in this case, what you did makes a lot of sense--especially the part about how the proc becomes the center of attention, and what exactly gets displayed becomes secondary. <BR/><BR/>Thanks for the tip.Wil Chttps://www.blogger.com/profile/03696320260631888445noreply@blogger.comtag:blogger.com,1999:blog-16002962.post-28116986363553571152007-08-31T09:44:00.000-07:002007-08-31T09:44:00.000-07:00Hey, that's much DRYer, but you could go further. ...Hey, that's much DRYer, but you could go further. To really use an *anonymous* function, you could just use Proc.new{ code } and then append .call() onto it. And you could use the ternary operator inside the call()'s parens to determine the argument.<BR/><BR/>i.e.<BR/><BR/>Proc.new {|label| ...code using label... }.call(test ? "this label" : "that label")<BR/><BR/>e.g.<BR/><BR/>if page.bookmarked_by? session[:user]<BR/> "Doing this"<BR/>else<BR/> Proc.new { |label|<BR/>link_to_remote "#{image_tag('heart_add.png',<BR/> :height => 16, :width => 16)} #{label}",<BR/> :url => { :controller =><BR/> :bookmark_list,<BR/> :action => :add,<BR/> :page_id => page.id },<BR/> :loading => "Element.toggle('#{indicator_id}')",<BR/> :complete => "Element.toggle('#{indicator_id}')"<BR/>}.call(page.done_by? session[:user] ? <BR/> "Bookmark again!" : "Bookmark!")<BR/>end<BR/><BR/><BR/>Some things to point out about this construction:<BR/><BR/>1.You never add to your namespace with a name for your proc.<BR/>2. The proc becomes the center of attention, at least to human readers, and the argument passed to it (and the reasons for passing it), become secondary.<BR/>3. It looks mighty weird at first, but actually it isn't much stranger than, say, <B>sprintf</B>, which also pushes the thing you're used to worrying about (what's the variable's value?) to the end of the line/block.<BR/><BR/>mhMichael Harrisonhttps://www.blogger.com/profile/01532019950536814477noreply@blogger.com