Why is my ruby class behaving unexpectedly? -


i have following ruby class:

class scheme   attr_reader :id, :uri, :labels, :in_schemes, :top_concepts    def initialize(id, uri, labels, in_schemes)     @id = id,     @uri = uri,     @labels = labels     @in_schemes = in_schemes     @top_concepts = array.new   end end 

and have following method traverses given directory looking files (they have names "01", "01.01.00", etc.) containing series of language-specific category labels (sample below):

def make_schemes(catdir)   concept_schemes = array.new   dir.foreach(catdir) |file|     unless file == "." || file == ".."       id = file.gsub(/\./,"").to_s       uri = "/unbist/scheme/#{id}"       labels = array.new       in_schemes = array.new       file.read("#{catdir}/#{file}").split(/\n/).each |line|         label = json.parse(line)         labels << label       end       if id.size > 2         in_schemes = ["/unbist","/unbist/#{id[0..1]}"]       else         in_schemes = ["/unbist"]       end       p "making new concept scheme id: #{id}"       concept_scheme = scheme.new(id, uri, labels, in_schemes)       p concept_scheme       concept_schemes << concept_scheme     end   end   return concept_schemes end 

sample category file, named "#{dir}/01". each line proper json, whole file, reasons beyond scope of question, not.

{ "text": "ﻢﺳﺎﺌﻟ ﻕﺎﻧﻮﻨﻳﺓ ﻮﺴﻳﺎﺴﻳﺓ", "language": "ar" } { "text": "政治和法律问题", "language": "zh" } { "text": "political , legal questions", "language": "en" } { "text": "questions politiques et juridiques", "language": "fr" } { "text": "ПОЛИТИЧЕСКИЕ И ЮРИДИЧЕСКИЕ ВОПРОСЫ", "language": "ru" } { "text": "cuestiones politicas y juridicas", "language": "es" } 

the output getting strange. id variable in make_schemes method set prior constructing new scheme, scheme initializer seems confused somewhere , applying entire set of variables object's id variable. here output above sample (cleaned newlines added readability:

"making new concept scheme id: 01" #<scheme:0xa00ffc8  @uri="/scheme/01",  @labels=[{"text"=>"مسائل قانونية وسياسية", "language"=>"ar"}, {"text"=>"政治和法律问题", "language"=>"zh"}, {"text"=>"political , legal questions", "language"=>"en"}, {"text"=>"questions politiques et juridiques", "language"=>"fr"}, {"text"=>"ПОЛИТИЧЕСКИЕ И ЮРИДИЧЕСКИЕ ВОПРОСЫ", "language"=>"ru"}, {"text"=>"cuestiones politicas y juridicas", "language"=>"es"}],  @id=["01", "/scheme/01", [{"text"=>"مسائل قانونية وسياسية", "language"=>"ar"}, {"text"=>"政治和法律问题", "language"=>"zh"}, {"text"=>"political , legal questions", "language"=>"en"}, {"text"=>"questions politiques et juridiques", "language"=>"fr"}, {"text"=>"ПОЛИТИЧЕСКИЕ И ЮРИДИЧЕСКИЕ ВОПРОСЫ", "language"=>"ru"}, {"text"=>"cuestiones politicas y juridicas", "language"=>"es"}]],  @in_schemes=["/"],  @top_concepts=[]> 

what missing here? causing this? have constructor different class works fine similar logic. i'm baffled. maybe there's approach work better?

try fixing:

@uri = uri, 

to:

@uri = uri 

as is, you're telling ruby:

@uri = uri, @labels = labels     

which, read it, means you're assigning labels array of uri, @labels, assigning array @uri.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -