What is the ruby way to do this?

On 11/23/05, David A. Black [email protected] wrote:

Hi –

On Wed, 23 Nov 2005, Lyndon S. wrote:

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 :slight_smile:

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.

David

Nice RaDD work ( Rapid Documentation Development ) !

On Wed, Nov 23, 2005 at 01:58:01PM +0900, Joel VanderWerf wrote:

It’s not completely useless:

irb(main):001:0> noargs = proc { || 3 }
=> #Proc:0xb7b3558c@:1(irb)
irb(main):002:0> noargs[]
=> 3
irb(main):003:0> noargs[2]
ArgumentError: wrong number of arguments (1 for 0)
from (irb):3
from (irb):3

Maybe not for long

[lambda{ }.arity, RUBY_VERSION] # => [0, “1.9.0”]

vs.

[lambda{ }.arity, RUBY_VERSION] # => [-1, “1.8.3”]

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 :wink: . 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 ???

j.


“Remember. Understand. Believe. Yield! → http://ruby-lang.org

Jeff W.

Hi –

On Wed, 23 Nov 2005, Joel VanderWerf wrote:

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:

noargs = Proc.new { || 3 }
=> #Proc:0x0033654c@:8(irb)
irb(main):009:0> noargs[2]
=> 3

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.

(Matz, please proceed with the removal of ‘proc’ :slight_smile:

David

Hi –

On Wed, 23 Nov 2005, Jeff W. wrote:

Anyways, You could seperate them if you didn’t want them to look like or …
like …

{ | | [] } … although that’s kinda ugly to me.

No argument there :slight_smile:

I’d be just as happy to use the new block syntax ->{ [] } … when is that
going to see the light of day ???

I think I’m not alone is saying: hopefully never :slight_smile: Anyway, you can
just do:

Hash.new { [] }

It’s a pretty common idiom, and the block is unambiguously a block.
(If it were a hash argument it would have to be in parentheses.)

David

Hi –

On Wed, 23 Nov 2005, Christophe G. wrote:

@attributes << a2
end
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.

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:

obj = Object.new

A = 1

class << obj
A = 2
def x
puts A
end
end

def obj.y
puts A
end

obj.x # 2
obj.y # 1

David

Selon “ako…” [email protected]:

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.

Christophe G…

http://rainbow.conlang.free.fr

It takes a straight mind to create a twisted conlang.

On Nov 22, 2005, at 5:45 PM, Bill K. wrote:

@attributes = Hash.new {|h,k| h[k] = Array.new}

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? :wink:

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).

foo = Hash.new{ [] }
foo[:bar] << 1
foo[:foo] << 2
foo[:bar] << 3
p foo
#=>{}

foo = Hash.new{ |h,v| h[v]=[] }
foo[:bar] << 1
foo[:foo] << 2
foo[:bar] << 3
p foo
#=>{:bar=>[1, 3], :foo=>[2]}

foo = Hash.new{ [] }
foo[:bar] <<= 1
foo[:foo] <<= 2
foo[:bar] <<= 3
p foo
#=>{:bar=>[1, 3], :foo=>[2]}

David A. Black wrote:

now ( heh, need to get that into the rubyscore app )… for my

It’s a pretty common idiom, and the block is unambiguously a block.
(If it were a hash argument it would have to be in parentheses.)

Note though that this idiom will most likely lead to surprising results
wrt the OP’s requirement. :slight_smile:

Cheers

robert