methods, methods override classes and scope

R

Ramza Brown

I am trying to figure out how scope works with method in methods (or
actually overriding).

For example, below with the:
def parser.startElement(n, data)

I want to pass top-level variables to it, but I am getting an error the
object is nil.

/html_extract.rb:72:in `startElement': undefined method
`meta_description=' for nil:NilClass (NoMet
odError)
from ./ymhtml.rb:1024:in `parse'
from ./html_extract.rb:95:in `html_parse'
from ./html_extract.rb:115:in `fetch_http_document'
from ./html_extract.rb:46:in `connect_http'
from html_extract_main.rb:12:in `html_extract_main'
from html_extract_main.rb:15


class HTMLMetadata

attr_writer :meta_description
attr_writer :meta_keywords

attr_writer :document_size
attr_writer :links

....
....

# Parse and extract
parser = YmHTML::parser.new
parser.eliminateWhiteSpace = true

@html_data = HTMLMetadata.new()

# Process only links and the meta tag (including keywords)
def parser.startElement(n, data)
case n

when 'meta'

# Extract the meta content including keywords
# and description
if data['name']
type_lower = data['name'].downcase
if type_lower == 'description'
puts data['content'].downcase
@html_data.meta_description = data['content'].downcase
end


--
Berlin Brown
(ramaza3 on freenode)
http://www.newspiritcompany.com
http://www.newspiritcompany.com/newforums
also checkout alpha version of botverse:
http://www.newspiritcompany.com:8086/universe_home
 
N

ndh.00008B

' @html_data = HTMLMetadata.new() '
When you do that, you are creating a class attribute, not an instance
attribute. you need to put that line of code into the initialize method
of your class
e.g.



class Person
@name
attr_accessor :name
end

class Test
@p=Person.new #class attribute
@p.name ='bob'

def initialize
@p =Person.new #instance attribute
@p.name ='joe'
end
def Test.p
@p.name
end
end

t=Test.new
def t.x
@p.name
end

puts Test.p #prints out bob
puts t.x #prints out joe
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top