Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在Ruby文件之间传递数组_Arrays_Ruby_Printing_Parameter Passing - Fatal编程技术网

Arrays 如何在Ruby文件之间传递数组

Arrays 如何在Ruby文件之间传递数组,arrays,ruby,printing,parameter-passing,Arrays,Ruby,Printing,Parameter Passing,我想将一个数组从一个Ruby文件传递到另一个Ruby文件 我有三个文件: main.rb company.rb 申请人.rb 下面是main.rb的代码: require './src/company.rb' require './src/applicant.rb' company = Company.new('data/boundless.json') company.find_applicants('google') 以下是company.rb的代码: require 'json'

我想将一个数组从一个Ruby文件传递到另一个Ruby文件

我有三个文件:

  • main.rb
  • company.rb
  • 申请人.rb
下面是main.rb的代码:

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

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

company.find_applicants('google')
以下是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
最后是applicator.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
程序读取JSON文件以从中获取一些信息。每当我试图打印发送到申请者文件的值时,都不会打印任何内容。

您不能将数组从ruby文件传递到另一个文件,只能在类和对象之间传递数据

其他可能有帮助的可能性:

  • 常量(以大写字母开头定义)
  • 全局变量(以$开头)
  • 单身汉
要将数据保存在类实例(对象)中,您需要属性(以
@
开头的变量)

你可以在每一本ruby初学者手册中找到这个概念(如果没有,那么这本手册就不值得使用)


你又犯了一个错误

让我们用一个小例子来检验一下:

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

puts Company.new.jobs
结果是一个空行

发生了什么事?在
initialize
-方法中定义一个局部变量
jobs
。局部平均值,仅在方法中可用。当方法离开时,ans丢失

正确的方法是1)使用实例变量:

class Company
  attr_accessor :jobs
  def initialize()
    @jobs='This should be assigned to my accessor jobs'
  end
end
或2)使用访问器方法:

class Company
  attr_accessor :jobs
  def initialize()
    self.jobs='This should be assigned to my accessor jobs'
  end
end
在这两种情况下,
放置Company.new.jobs
返回您定义的文本


另请参见

如果我没有看错,您要求ruby进行计算,但从未声明应该打印。我相信将main.rb的最后一行更改为:

puts company.find_applicants('google')

应该足够了。

我建议使用一个小型数据库,可能是SQLite,它充当数据的公共存储库。Sequel是一个很好的ORM,使其易于使用。或者,您可以使用YAML文件,但这不是YAML的好用途,因为一个文件对该文件的更改如果在读/写过程中发生冲突,可能会对另一个文件造成严重破坏。您还可以通过管道将文件连接在一起,并在它们之间传递数据。通常,您需要正确格式化代码。它帮助您调试,也帮助我们帮助您。Ruby变量是snake_,而不是Camelcase。我看不到试图重写数据文件的代码。不要让我们为你写。我们很乐意帮助你调试这个问题,但向我们展示你在这方面的努力。@Thetman我不是要你写任何东西,我只是问为什么它不传递数据,也就是说它不传递任何东西,因为你没有告诉它。我已经将申请者文件包括在公司文件中,并调用了该方法,申请人的数据来自申请人的方法数据,我已经从他们那里调用了申请人的方法数据main@kamal投票支持和/或接受有帮助的答案。这里就是这样