It would be nice if there was a wiki page somewhere of these
Uberdioms…
I’m not sure what you mean. A wiki page of… ?
Sorry
Uberdioms = Uber Idioms = Made up word representing powerfull and
sometimes
lacking in immediate clarity language constructs that programmers should
be aware of but not necessarily utilising in all circumstances.
Heh, and to think the only reason I did it was because I figured just
having
the { [] } would confuse things ( badly formed hash declaration ) so I
put
the goalposts in just to be sure that it saw things as a block body …
I’ll gladly take credit, I’m glad I’ll be remembered for something now (
heh, need to get that into the rubyscore app )… for my “empty
goalpost”
strategem . I’m not Dave T. or DHH or _why … or matz … or any
of
the countless others that have helped me better myself as a Rubyist, but
hopefully one day I’ll get to have a big impact … and pay them all
back. I
thank you all everyday I get to play with Ruby.
Anyways, You could seperate them if you didn’t want them to look like or
…
like …
{ | | [] } … although that’s kinda ugly to me.
I’d be just as happy to use the new block syntax ->{ [] } … when is
that
going to see the light of day ???
I believe that’s the first empty || I’ve ever seen. It’s awfully
irb(main):003:0> noargs[2]
ArgumentError: wrong number of arguments (1 for 0)
from (irb):3
from (irb):3
Interesting. I wonder why. And it’s yet another case where
proc/lambda and Proc.new are different:
It seems very fragile to me as a way of communicating what’s going on.
There are two ways of saying “This takes no arguments”, and what they
mean depends on the Proc/proc distinction. I guess this is old news
– it’s just another twist, and one I hadn’t noticed before, on that
whole area.
The short answer is “yes”. The long asnwer is that both are ways to add a
singleton method to a particular object, @attributes in this case. The first
one is just shorter and practical when you want to create just one singleton
method. But it’s mostly a matter of style.
There’s also a (usually not too important) difference involving the
scope of constants. The << version will see constants in the
singleton class, whereas the def obj.meth version won’t:
this is something i have never seen. would you explain the ‘def’ line
above? is this the same as class << @attributes; def << …; end ?
If I understand correctly you are asking whether:
def @attributes.<<(obj)
…
end
is the same as:
class << @attributes
def <<(obj)
…
end
end
(I rewrote them because the clash of << as syntax and << as method made
it a bit
difficult to read).
The short answer is “yes”. The long asnwer is that both are ways to add
a
singleton method to a particular object, @attributes in this case. The
first
one is just shorter and practical when you want to create just one
singleton
method. But it’s mostly a matter of style.
Hey, thanks for that! I had no idea that the block form of Hash
creation had block arguments. Sure it’s in ‘ri’, but who looks there?
I often see people suggesting:
Hash.new{ [] }
without realizing that this doesn’t set the value on the hash, just
returns a blank array. I’ve never been able to use the block
constructor syntax because it was so useless (because I also didn’t
know that <<= was a valid compound method).