Alias or alias_method problem

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
return _[nil] if ag.size<1
_[*ag]
end
end

Thanks,
Angel

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

end

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
return old_bracket(nil) if ag.size<1
old_bracket(*ag)
end
end

Thanks,
Angel

You’re welcome!

···

On Thu, 29 Apr 2004 08:36:03 +0900, Angel Martin wrote:

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

···

El jue, 29-04-2004 a las 02:29, Kristof Bastiaensen escribió:

On Thu, 29 Apr 2004 08:36:03 +0900, Angel Martin wrote:

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
return old_bracket(nil) if ag.size<1
old_bracket(*ag)
end
end

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

···

— Kristof Bastiaensen kristof@vleeuwen.org wrote:

Hi,

On Thu, 29 Apr 2004 08:36:03 +0900, Angel Martin wrote:

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


Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
http://hotjobs.sweepstakes.yahoo.com/careermakeover

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

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

···

On Thu, Apr 29, 2004 at 01:18:38PM +0900, Jeff Mitchell wrote:

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


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

_ is a valid variable name.

-austin

···

On Thu, 29 Apr 2004 10:11:42 +0900, Angel Martin wrote:

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


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2004.04.28
* 22.37.52

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

Yours,

Jean-Hugues

···

At 16:07 29/04/2004 +0900, Mauricio Fernández batsman.geo@yahoo.com wrote:

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


Web: @jhr is virteal, virtually real
Phone: +33 (0) 4 92 27 74 17

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

···

On Thu, 29 Apr 2004 10:11:42 +0900, Angel Martin wrote: