alias or alias_method problem

A

Angel Martin

Hi all,
Is a way to make this code work:

class Hash #hash[] === hash[nil]
alias_method "_[]=","[]="
alias_method "_[]" ,"[]"
def []=(*ag)
value=ag.pop
return _[nil]=value if ag.size<1
_[*ag]=value
end
def [](*ag)
return _[nil] if ag.size<1
_[*ag]
end
end

Thanks,
Angel
 
K

Kristof Bastiaensen

Hi,

alias_method "_[]=","[]="
alias_method "_[]" ,"[]"

I guess renaming to something with "[]" in the name
will not work, (due to the way Ruby compiles expressions).
_[*ag]=value

Will try to evaluate _, and then calls []= on the result.
(It seems that _ evaluates to nil inside irb,
I don't understand why...)

Try the following instead:

class Hash #hash[] === hash[nil]
alias_method "old_bracket_assign", "[]="
alias_method "old_bracket" ,"[]"
def []=(*ag)
value=ag.pop
return old_bracket_assign(nil, value) if ag.size<1
old_bracket_assign(*(ag.push(value)))
end
def [](*ag)
return old_bracket(nil) if ag.size<1
old_bracket(*ag)
end
end
Thanks,
Angel

You're welcome!
 
A

Angel Martin

El jue, 29-04-2004 a las 02:29, Kristof Bastiaensen escribió:
alias_method "_[]=","[]="
alias_method "_[]" ,"[]"
Try the following instead:

class Hash #hash[] === hash[nil]
alias_method "old_bracket_assign", "[]="
alias_method "old_bracket" ,"[]"
def []=(*ag)
value=ag.pop
return old_bracket_assign(nil, value) if ag.size<1
old_bracket_assign(*(ag.push(value)))
end
def [](*ag)
return old_bracket(nil) if ag.size<1
old_bracket(*ag)
end
end

No work :-(, raise a SyntaxError in the last line of the file:
/ghost.rb:2:in `require': ./manplug.rb:191: syntax error (SyntaxError)
from ./ghost.rb:2
My Hash code is about line 125.
I don't understand why it protest with a SyntaxError :-?

Thanks for reply.
Regards,
Angel
 
A

Angel Martin

Try the following instead:
class Hash #hash[] === hash[nil]
alias_method "old_bracket_assign", "[]="
alias_method "old_bracket" ,"[]"
def []=(*ag)
value=ag.pop
return old_bracket_assign(nil, value) if ag.size<1
old_bracket_assign(*(ag.push(value)))
end
def [](*ag)
return old_bracket(nil) if ag.size<1
old_bracket(*ag)
end
end

No work :-(, raise a SyntaxError in the last line of the file:
./ghost.rb:2:in `require': ./manplug.rb:191: syntax error (SyntaxError)
from ./ghost.rb:2
My Hash code is about line 125.
I don't understand why it protest with a SyntaxError :-?
because I forget to change some code in the file :)
Ok, works great but when i tried:
return _[]=(nil, value) if ag.size<1
it protest with a aprox: "cannot assign to nil"

A lot of thanks,
regards
 
M

Mauricio Fernández

99% of the time alias_method is used in conjunction with
redefining a method. The above issues would have been
avoided if ruby had something like,

redef foo(*args)
puts "new foo"
previous(*args) # calls old foo
end


batsman@tux-chan:/tmp$ cat redef.rb
class Module
def redef(meth, &block)
prev = self.instance_method(meth)
define_method(meth) do |*a|
block.call prev.bind(self), *a
end
end
end

class A
def foo(*a)
puts "A#foo #{a.inspect}"
end
end

a = A.new
a.foo

class A
redef :foo do |prior, *a|
puts "new foo"
prior[*a]
end
end

a.foo
batsman@tux-chan:/tmp$ ruby redef.rb
A#foo []
new foo
A#foo []


More details in [ruby-talk:97440].

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Those who don't understand Linux are doomed to reinvent it, poorly.
-- unidentified source
 
J

Jean-Hugues ROBERT

At 16:07 29/04/2004 +0900 said:
class Module
def redef(meth, &block)
prev = self.instance_method(meth)
define_method(meth) do |*a|
block.call prev.bind(self), *a
end
end
end

More details in [ruby-talk:97440].

Cool. I added it (with some refactoring) to my software tools.
Thanks Mauricio.

Yours,

Jean-Hugues
 
K

Kristof Bastiaensen

Hi,

Try the following instead:

class Hash #hash[] === hash[nil]
alias_method "old_bracket_assign", "[]="
alias_method "old_bracket" ,"[]"
def []=(*ag)
value=ag.pop
return old_bracket_assign(nil, value) if ag.size<1
old_bracket_assign(*(ag.push(value)))
end
def [](*ag)
return old_bracket(nil) if ag.size<1
old_bracket(*ag)
end
end
Ok, works great but when i tried:
return _[]=(nil, value) if ag.size<1
it protest with a aprox: "cannot assign to nil"

If you write _[]=(nil, value), ruby will interprete it
as:

(_)[] = (nil, value)
#meaning
(_).[]=((nil, value))

Which is invalid syntax, because (nil, value) as an expression
is illegal.
(The "cannot assign to nil" response is kind of a mystery to me...)

When written as:

_.[]=(nil, value)

it will work, assuming _ contains something useful.

for example:

_ = {}
_.[]=(nil, 4)
_[nil]
=> 4
regards
Angel,

Hope this explanation helps.

Kristof
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top