From: Tom Cloyd Date: 2009-01-05T21:02:21+09:00 Subject: Re: VERY simple question about "?" Brian Candler wrote: > Tom Cloyd wrote: > >> * Operators - has the same problem as Thomas' book: precedence is shown, >> but basic operator function appears to be assumed to be known. I'm >> sorry, but I wasn't born with that knowledge. Look at this line: <=> == >> === != =~ !~ >> > > There aren't really any operators in Ruby. Ruby simply maps all these to > method calls (*), so to find out what the operator *means* you have to > look at the class documentation for that method. > > e.g. > str1 = "foo" > str2 = "bar" > p str1 <=> str2 > > Here you need to look at the documentation for String#<=> to see what it > means. Yes, "<=>" is a legitimate method name :-) You can also call it > like this: > > str1.<=>(str2) > > The only real purpose of the operator precedence table is to show how > expressions containing multiple operators are resolved. e.g. > > 1 + 2 * 3 > > # same as: > # 1 + (2 * 3) > > # same as: > # 1.+(2.*(3)) > > Note that even the methods '+' and '*' do not necessarily have anything > to do with addition or multiplication. It's entirely dependent on the > target object how it implements these methods. For example: > > "123" * 3 # => "123123123" > > HTH, > > Brian. > > (*) The exceptions in ruby 1.8 are != and !~, which invoke the methods > == and =~ but then negate the result > Ah...This is actually very helpful. I think we have a language problem, though. If there are no operators in Ruby, we should call all those methods with non-alphabetical names something besides "operators". I know...we get USED to nonsense. Every try to learn to spell English? I still don't do it well. But we live with it. Here, I think we need to change not only the computer language (experts appear to be evolving it quite nicely, and I'm happy just to watch), but the human language we use to talk about it. I guess this particularly matters to me because I write a lot - daily. I virtually live at a keyboard. Verbal coherence matters a lot to me, even if I can't always achieve it. OK, let's assume we have the language problem solved (ha! - not likely to happen). Nann-methods == "non-alphabetically-named methods". The core problem still remains - what the blazes does that THING do/mean/produce...??? Here's were your post is helpful to me. I will follow your suggestion. I feel better already, because for the first time in a long time [a] I understand my confusion, AND [2] what to do about it. A whole new world. Dave...if you're still listening...it'd be lovely if some clarification along the lines of this post made it into your 3rd edition - that's what I find myself thinking, anyway. Thanks to all...for hanging with me on this one. Thanks. T. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << tc@tomcloyd.com >> (email) << TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~