Similar Threads:
1.Set doesn't have [] instance method
It should, shouldn't it? It's meant to combine the fast lookup of
Hash with the convenience of Array, yet the most important method Hash
and Array have in common, Set lacks!
$ irb
irb(main):001:0> require 'set'
=> true
irb(main):002:0> s = Set[1,2,3]
=> #<Set: {1, 2, 3}>
irb(main):003:0> s.include? 1
=> true
irb(main):004:0> s[1]
NoMethodError: undefined method `[]' for #<Set: {1, 2, 3}>
from (irb):4
from :0
This would be a good thing to fix before 1.8 is released.
Gavin
2.Set doesn't have [] instance method - and Set#| is inconsistent with Set#&
On Sunday, July 27, 2003, 3:28:06 AM, Gavin wrote:
> It should, shouldn't it? It's meant to combine the fast lookup of
> Hash with the convenience of Array, yet the most important method Hash
> and Array have in common, Set lacks!
> [...]
Oh well, judging by the responses, I guess it's not all that sensible
after all.
I just don't like typing "include?" for something as simple as a lookup,
especially when Set (to my mind) is just a special case of Hash --
with a bit more thrown in. Also since "include?" is bad grammar.
Another question, then. Why does Set#| return a Set, but Set#&
returns an Array?
Gavin
3.Method equality; setting instance variables on Method instances
A few questions about the code below:
1) Why do m1 and m2 have different IDs but compare as equal?
2) Why does m2 invoke the same old functionality when run?
3) Why did my instance_variable_set not 'take'?
I suspect all answers have something to do with where the methods are
being defined/shadowed, but I'll be damned if I can figure them out.
slim:~/Desktop/test gavinkistner$ ls -Flag
total 8
drwxr-xr-x 3 staff 102 Dec 24 10:15 ./
drwx------ 34 staff 1156 Dec 24 10:15 ../
-rw-r--r-- 1 staff 734 Dec 24 10:20 override.rb
slim:~/Desktop/test gavinkistner$ cat override.rb
class Module
def override( method_name, &block )
orig_method = method( method_name )
new_name = "m#{rand(1000000000)}"
alias_method new_name, method_name
define_method( method_name, &block )
method( method_name ).instance_variable_set( :@__orig_method_name,
new_name )
end
def restore( method_name )
alias_method method_name, method( method_name
).instance_variable_get( :@__orig_method_name )
end
end
m1 = Kernel.method( :system )
system 'ls'
Kernel.override( :system ){ |code| puts "`#{code}` not allowed"}
m2 = Kernel.method( :system )
system 'ls'
p m1, m2, m1==m2, m1.object_id, m2.object_id
m1.call( 'ls' )
m2.call( 'ls' )
Kernel.restore( :system )
m3 = Kernel.method( :system )
system 'ls'
__END__
slim:~/Desktop/test gavinkistner$ ruby override.rb
override.rb
`ls` not allowed
#<Method: Kernel.system>
#<Method: Kernel.system>
true
953400
953250
override.rb
override.rb
override.rb:11:in `alias_method': nil is not a symbol (TypeError)
from override.rb:11:in `restore'
from override.rb:26
4.instance method adding another instance method to the class
5.rdoc, link to another class instance method
6. "ruby myscript.rb" Works, "./myscript.rb" Doesn't
7. including instance methods and setting an instance varia
8. including instance methods and setting an instance variable