Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > redefining splat?

Reply
Thread Tools

redefining splat?

 
 
Adam Shelly
Guest
Posts: n/a
 
      10-02-2006
Can you redefine the * prefix operator?
I'm in the middle of refactoring some code. I have a function that
looks like this

def send_command cmd_arry
type = cmd_arry.shift
@comms.send(type, *cmd_arry)
end

I want to replace the command arrays (which have the form [:COMMAND,
data1, data2, ...]) with a class structure like:

class LogonCommand < Command
def initialize name, pass
super()
@name,@pass = name,pass
@command = :LOGON
end
def write
[@command, @name, @pass]
end
end

but there are a lot of these command types, and I wanted to do a
gradual transition. So I thought I could make the Command class mimic
the array:

class Command
def shift
@command
end
def *
*(write[1..-1])
end
end

but that gives a syntax error. Is there any way to redefine the splat
for my class? Or at least to make my class act like an array when the
splat is applied?

-Adam

 
Reply With Quote
 
 
 
 
Florian Frank
Guest
Posts: n/a
 
      10-02-2006
Jeremy Kemper wrote:
> You can define to_ary which splat uses. In Ruby 1.9 you can define
> to_splat

You mean #to_a?

--
Florian Frank


 
Reply With Quote
 
 
 
 
Adam Shelly
Guest
Posts: n/a
 
      10-02-2006
On 10/2/06, Jeremy Kemper <(E-Mail Removed)> wrote:
> You can define to_ary which splat uses. In Ruby 1.9 you can define to_splat
> directly.
>


perfect, thanks.

 
Reply With Quote
 
Brian Mitchell
Guest
Posts: n/a
 
      10-02-2006
On 10/2/06, Florian Frank <(E-Mail Removed)> wrote:
> Jeremy Kemper wrote:
> > You can define to_ary which splat uses. In Ruby 1.9 you can define
> > to_splat

> You mean #to_a?


class C
def to_a
[:to_a]
end
end

a = *C.new

class C
def to_ary
[:to_ary]
end
end

b = *C.new

puts a, b

Brian.

 
Reply With Quote
 
MonkeeSage
Guest
Posts: n/a
 
      10-02-2006
Brian Mitchell wrote:
> class C
> def to_a
> [:to_a]
> end
> end
>
> a = *C.new
> ...


Looks for #to_ary first, failing that uses #to_a:

class C
def to_a
[:to_a]
end
def to_ary
[:to_ary]
end
end

p *C.new

Regards,
Jordan

 
Reply With Quote
 
Brian Mitchell
Guest
Posts: n/a
 
      10-02-2006
On 10/2/06, MonkeeSage <(E-Mail Removed)> wrote:
> Brian Mitchell wrote:
> > class C
> > def to_a
> > [:to_a]
> > end
> > end
> >
> > a = *C.new
> > ...

>
> Looks for #to_ary first, failing that uses #to_a:
>
> class C
> def to_a
> [:to_a]
> end
> def to_ary
> [:to_ary]
> end
> end
>
> p *C.new
>


As was my point . There was a reason I reopened the class.

Brian.

 
Reply With Quote
 
Florian Frank
Guest
Posts: n/a
 
      10-02-2006
Brian Mitchell wrote:
> On 10/2/06, Florian Frank <(E-Mail Removed)> wrote:
>> Jeremy Kemper wrote:
>> > You can define to_ary which splat uses. In Ruby 1.9 you can define
>> > to_splat

>> You mean #to_a?

>

Well, #to_ary expresses a bit more than just splatability (is this even
a word?). It means that the Object is in some way actually an Array, not
only that it can be converted into an array if needed. A good example
for this is Pathname#to_str.

So, you don't have to use the splat operator at all, if you want to
print a class that implements #to_ary, e. g.:

>> class A;def to_ary;[:foo,:bar];end;end

# => nil
>> puts A.new

foo
bar

This is different from:

>> class B;def to_a;[:foo,:bar];end;end

# => nil
>> puts *B.new

foo
bar
>> puts B.new

#<B:0xb7a8d114>

--
Florian Frank


 
Reply With Quote
 
MonkeeSage
Guest
Posts: n/a
 
      10-02-2006
Brian Mitchell wrote:
> As was my point . There was a reason I reopened the class.


Just trying to emphasize your point.

Regards,
Jordan

 
Reply With Quote
 
Matt Todd
Guest
Posts: n/a
 
      10-03-2006
> Can you redefine the * prefix operator?

Why do you need to? I know slight modifications to do some cool
wizardry can be adventageous, but to me, what it seems like you want
is a completely different thing that will completely redefine the *.
(Even if only locally.)

> I'm in the middle of refactoring some code. I have a function that
> looks like this
>
> def send_command cmd_arry
> type = cmd_arry.shift
> @comms.send(type, *cmd_arry)
> end


What's wrong with...

def send_command cmd_ary
cmd = Command.new(cmd_ary)
@comms.send(cmd.type, cmd.params)
end

Or even...

def send_command cmd_ary
cmd = Command.new(cmd_ary)
@comms.send cmd
end

with #send overwritten to do something special with the Command class?
(Still retaining the original functionality otherwise.)

Just my opinion.

M.T.

 
Reply With Quote
 
Adam Sanderson
Guest
Posts: n/a
 
      10-03-2006
I would agree with Matt, if you are trying to redefine the splat
operator (is that really what we're calling it? neat), with the .to_a
or .to_ary to return a class instead of an array, you should make sure
that the class looks enough like an array that it wouldn't break
someone's expectations later.

It might not make a big difference now, but should someone need to
touch your code later, even if that person is yourself, you will save
many headaches by not being overly trixy.
.adam



Matt Todd wrote:
> > Can you redefine the * prefix operator?

>
> Why do you need to? I know slight modifications to do some cool
> wizardry can be adventageous, but to me, what it seems like you want
> is a completely different thing that will completely redefine the *.
> (Even if only locally.)
>
> > I'm in the middle of refactoring some code. I have a function that
> > looks like this
> >
> > def send_command cmd_arry
> > type = cmd_arry.shift
> > @comms.send(type, *cmd_arry)
> > end

>
> What's wrong with...
>
> def send_command cmd_ary
> cmd = Command.new(cmd_ary)
> @comms.send(cmd.type, cmd.params)
> end
>
> Or even...
>
> def send_command cmd_ary
> cmd = Command.new(cmd_ary)
> @comms.send cmd
> end
>
> with #send overwritten to do something special with the Command class?
> (Still retaining the original functionality otherwise.)
>
> Just my opinion.
>
> M.T.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redefining an enumerated attribute type Nick Bassiliades XML 1 12-12-2005 01:21 PM
Redefining operators in C++ bberu C++ 7 12-18-2004 08:32 AM
Redefining operators in C++ bberu C++ 1 12-17-2004 01:04 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
redefining cout (for using printf/mexprintf) uli C++ 5 04-21-2004 04:04 AM



Advertisments