# Accessing a class method from within a base class

**URL:** <https://www.ruby-forum.com/t/accessing-a-class-method-from-within-a-base-class/61801>\
**Category:** Ruby\
**Created:** [May 27, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/accessing-a-class-method-from-within-a-base-class/61801 "2006-05-27T18:24:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [May 27, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/accessing-a-class-method-from-within-a-base-class/61801/1 "2006-05-27T18:24:07Z")

</div>

Hi,

When I run the code at the end of the message it all works as expected  
but I get a warning :

workpad2.rb:3: warning: Object#type is deprecated; use Object#class

This is casued by the first line in the BaseClass.initialize method,  
because I use the type property to get to actual type of the class  
instance. If I use the “class” property as suggested in the warning,  
the code actually fails to compile with the following :

workpad2.rb:3: parse error, unexpected ‘.’  
if(!class.isMatch(input))  
^  
This occurs when I change the initialize method to use the “class”  
accessor:

```
def initialize(input)
	if(!class.isMatch(input))
		raise "Invalid input provided"
	end
end

```

Any advice? Should I just ignore the warning and go-ahead and use the  
“type” accessor?

Nick.

The code is:

class BaseClass  
def initialize(input)  
if(!type.isMatch(input))  
raise “Invalid input provided”  
end  
end  
end

class AClass \< BaseClass  
def initialize(input)  
super(input)  
end

```
def self.isMatch(input)
	puts "AClass::invoked"
	input =~ "1"
end

```

end

class BClass \< BaseClass  
def initialize(input)  
super(input)  
end  
def self.isMatch(input)  
puts “BClass::invoked”  
input == “2”  
end

end

bclass = BClass.new(“2”)  
puts “here”

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [May 27, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/accessing-a-class-method-from-within-a-base-class/61801/2 "2006-05-27T19:00:37Z")

</div>

On May 27, 2006, at 12:24 PM, Nick wrote:

> If I use the “class” property as suggested in the warning,  
> the code actually fails to compile with the following :
> 
> workpad2.rb:3: parse error, unexpected ‘.’  
> if(!class.isMatch(input))

You have to write this as:

if (!self.class.isMatch(input))

The reason is simply because the parser thinks ‘class’ all by itself  
is an attempt to open a class block as in:

class Foo

# 

end

by using self.class instead the parser sees it as a method call. It  
is an unfortunate syntax ambiguity that has to be manually  
disambiguated to avoid the error.

Gary W.

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [May 27, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/accessing-a-class-method-from-within-a-base-class/61801/3 "2006-05-27T19:47:37Z")

</div>

unknown wrote:

> On May 27, 2006, at 12:24 PM, Nick wrote:  
> …  
> You have to write this as:
> 
> if (!self.class.isMatch(input))

Gary,

I could have sworn I tried that! Its working now. I’ve been coding  
since 1pm so I guess I took a break at the right time, right after  
posting the message.

Many thanks,

Nick.
