-1

I want to pass an array from a Ruby file to another one.

I have a three files:

  • main.rb
  • company.rb
  • applicant.rb

Here is the code for main.rb:

require './src/company.rb'
require './src/applicant.rb'

company = Company.new('data/boundless.json')

company.find_applicants('google')

Here is the code for company.rb:

require 'json'
require_relative 'applicant.rb'


class Company
  attr_accessor :jobs , :arrOfApp

  def self.load_json(filepath)

    file = File.read(filepath)
    return JSON.parse(file)
  end

  def initialize(filePath)
    # Load the json file and loop over the jobs to create an array of instance of `Job`
    # Assign the `jobs` instance variable.

    jobs=Array.new
    data_hash = Company.load_json(filePath)

    numberOfJobs= data_hash['jobs'].length

    for  i in  0 ... numberOfJobs

      jobs[i]=data_hash['jobs'][i]['applicants']

      # puts jobs

    end

  end

  ## TODO: Impelement this method to return applicants from all jobs with a
  ## tag matching this keyword
  def find_applicants(keyWord)
    app =Applicant.new
    arrOfApp=Array.new
    app.data_of_applicant(jobs)
  end
end

And finally the code for applicant.rb:

require_relative 'company.rb'

class Applicant
  attr_accessor :id, :name, :tags

  def initialize

  end

  def data_of_applicant(j)
    id=Array.new
    name=Array.new
    tags=Array.new


    puts j


  end
end

The program reads a JSON file to get some information from it. Whenever I try to print the value being sent to the applicant file nothing is printed.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
john
  • 25
  • 1
  • 8
  • I'd recommend using a small database, possibly SQLite, which acts as a common repository of the data. Sequel is a great ORM that makes it easy to use. Or, you could use a YAML file, but that's not a good use of YAML as changes to the file by one file could cause havoc to another if they collide during a read/write. You could also pipe the files together and pass data between them. In general you need to format your code correctly. It helps you debug and it helps us help you. Ruby variables are in snake_case, not camelCase – the Tin Man Feb 04 '16 at 21:48
  • I see no code where you try to rewrite the data file. Don't ask us to write it for you. We're happy to help you debug the problem but show us your effort for that part. – the Tin Man Feb 04 '16 at 21:57
  • @theTinMan I'm not asking you to write anything, I'm just asking why it's not passing the data, that's it – john Feb 04 '16 at 22:05
  • It's not passing anything because you didn't tell it to. – the Tin Man Feb 04 '16 at 22:05
  • I have include the applicant file in the company file and called the method, data_of_applicant from the method data_of_applicant which I already called from them main – john Feb 04 '16 at 22:08

2 Answers2

0

You can't pass an array from a ruby file to another one., you only can pass data between classes and objects.

Other possibilities which may help:

  • constants (Defined with starting capital letter)
  • global variables (starting with $)
  • Singletons

To keep data inside the class instances (objects) you need attributes (variables starting with @).

You can find this concepts in every beginner manual of ruby (and if not, then the manual is not worth to be used)


You made another common error.

Let's check it with a small example:

class Company
  attr_accessor :jobs
  def initialize()
    jobs='This should be assigned to my accessor jobs'
  end
end

puts Company.new.jobs

The result is an empty line.

What happend? In the initialize-method you define a local variable jobs. Local means, it is only available in the method ans is lost when the method leaves.

Correct would be 1) using the instance variable:

class Company
  attr_accessor :jobs
  def initialize()
    @jobs='This should be assigned to my accessor jobs'
  end
end

or 2) using the accessor method:

class Company
  attr_accessor :jobs
  def initialize()
    self.jobs='This should be assigned to my accessor jobs'
  end
end

In both cases the puts Company.new.jobs returns the text you defined.

See also Ruby instance variable access

Community
  • 1
  • 1
knut
  • 27,320
  • 6
  • 84
  • 112
0

if i'm reading this correctly, you're asking ruby to make the calculation, but never stating that it should be printed. i believe changing the last line of your main.rb to this:

puts company.find_applicants('google')

should suffice.

ian root
  • 349
  • 1
  • 11