Method Scopes

T

Trans

I would like to see Ruby gain a "scoping feature" (I usually refer to
this as a "namespace", but looking at Matz' discussion of Rite he's
using that to mean something else). This idea is this:

class String

scope format

def jumble
j = ''
split(//).each_with_index{ |c,i| j << ( i % 2 == 0 ? c.downcase
: c.upcase ) }
j
end

end

end

"Wizard".format.jumble #=> wIzArD


Currently I can implement like so:

require 'delegate'

class String

def format
@__format__ ||= Format.new( self )
end

class Format < DelegateClass( self )

def jumble
j = ''
split(//).each_with_index{ |c,i| j << ( i % 2 == 0 ? c.downcase
: c.upcase ) }
j
end

end

end

But that has a whole lot of icky overhead. I would be nice if Ruby
could support this kind of thing natively. It's a nice way to organize
methods.

T.
 
J

Jesse Merriman

Hi,

I tried joining an Array of Arrays, and it seems the Array
is being flattened first or something.

ri Array#join tells me:
Returns a string created by converting each element of the array to
a string, separated by _sep_.

Its the part about each element getting converted to a string that
seems wrong:
x = [[1, 2], [3, 4]]
print x[0].to_s, ' ', x[1].to_s #=> "12 34"
x.join(' ') #=> "1 2 3 4"

I expected join to produce "12 34"; why doesn't it?
 
J

Jason Foreman

Hi,
=20
I tried joining an Array of Arrays, and it seems the Array
is being flattened first or something.
=20
ri Array#join tells me:
Returns a string created by converting each element of the array to
a string, separated by _sep_.
=20
Its the part about each element getting converted to a string that
seems wrong:
=20
x =3D [[1, 2], [3, 4]]
print x[0].to_s, ' ', x[1].to_s #=3D> "12 34"
x.join(' ') #=3D> "1 2 3 4"
=20
I expected join to produce "12 34"; why doesn't it?
=20

This boggled me for a while, had to actually look at the source for Array#j=
oin.

It does not simply call to_s when the sub-element is an array.=20
Essentially, it does a recursive call to join for elements that are
themselves arrays, passing in the original separator (' ' in this
case).


Jason
 
A

Austin Ziegler

I would like to see Ruby gain a "scoping feature" (I usually refer to
this as a "namespace", but looking at Matz' discussion of Rite he's
using that to mean something else). This idea is this:

Um. Ew?

Sorry, but I find that whole concept ("selector namespaces", I think,
is the common name for it) code-ugly.

To me, "Wizard".format.jumble means that I'm calling the method
#format on "Wizard" and then the method #jumble on the result of
#format. To have it mean something else randomly is, to say the least,
confusing.

I'd rather something clearly ugly for this concept, e.g.:

"Wizard".format:jumble

Or something. Better to be explicit and ugly than hidden and uglier.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
T

Trans

Austin said:
Um. Ew?

Sorry, but I find that whole concept ("selector namespaces", I think,
is the common name for it) code-ugly.

Ah, "selector namespace", thanks.
To me, "Wizard".format.jumble means that I'm calling the method
#format on "Wizard" and then the method #jumble on the result of
#format. To have it mean something else randomly is, to say the least,
confusing.

That's exaclty it though. Its very much like method chaining --i.e.
when a method returns self. I suspect you don't consider method
chaining ugly and confusing. So it makes sense if you think of it like
that. The only difference in this case is that the subsequent callable
methods are a select set.

T.
 
C

Charles Steinman

Trans said:
I would like to see Ruby gain a "scoping feature" (I usually refer to
this as a "namespace", but looking at Matz' discussion of Rite he's
using that to mean something else). This idea is this:

class String

scope format

def jumble
j = ''
split(//).each_with_index{ |c,i| j << ( i % 2 == 0 ? c.downcase
: c.upcase ) }
j
end

end

end

"Wizard".format.jumble #=> wIzArD


Currently I can implement like so:

require 'delegate'

class String

def format
@__format__ ||= Format.new( self )
end

class Format < DelegateClass( self )

def jumble
j = ''
split(//).each_with_index{ |c,i| j << ( i % 2 == 0 ? c.downcase
: c.upcase ) }
j
end

end

end

But that has a whole lot of icky overhead. I would be nice if Ruby
could support this kind of thing natively. It's a nice way to organize
methods.

I wouldn't be hard to create a Class#scope method that could do syntax
very much like your first example. Just have it create a delegate class
and instance_eval a passed block so it's executed in the scope of that
class.
 
J

Jesse Merriman

Hi Jason,

Yeah, I figured as much, but I think the documentation ought
to note this.
This boggled me for a while, had to actually look at the source for
Array#join.
=20
It does not simply call to_s when the sub-element is an array.=20
Essentially, it does a recursive call to join for elements that are
themselves arrays, passing in the original separator (' ' in this
case).
=20
=20
Jason

--
Jesse Merriman
(e-mail address removed)

--=20
http://www.fastmail.fm - Same, same, but different=85
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top