'<<' operator

I have a small doubt on wht the ‘<<’ operator do in ruby

for instance in this code

class << ro
undef :instance_eval
end

Hi,

This has nothing to do with the << operator. It’s the syntax of the
eigenclass of ro.

Ok thanks for the reply Jan,
Can u please ellaborate on the eigenclass and wht is it used for.

The eigenclass contains all singleton methods of an object. Those are
methods which don’t come from the class of the object but actually
belong to the object itself.

For example:

class A
def f
puts 1
end
end

a_1, a_2 =
A.new, A.new

define a singleton method for a_1

def a_1.g
puts 2
end

call instance method f (defined in class A)

a_1.f
a_2.f

call singleton method g (defined in singleton class of a_1)

a_1.g
a_2.g # this doesn’t work, because a_2 doesn’t have the method

Instead of writing def a_1.g, you could also explicitly define the
method in the eigenclass of a_1:

class << a_1
def g
puts 2
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs