在Ruby中访问对象内的属性

时间:2015-05-19 07:32:42

标签: ruby

我创建了一个Class并创建了许多实例并将其存储到名为messages

的数组中

如果我输入,

print messages[0]

它给出了

#<SMSMessage:0x00000001681d88 @id=29185, @account_id=1565, @upload_id=0, @txaction_type_id=9, @txid=0, @finanalytics_txid=1565,
 @date_posted=2015-05-13 17:58:01 +0530, @sequence=1, @sic_code=0,
 @amount=-1802.0, @raw_name="MERLINS", @filtered_name="MERLINS", 
@cleaned_name="NULL", @merchant_id="NULL", @memo="NULL", 
@check_num="NULL", @ref_num="NULL", @balance=0.0, @created_at=2015-05-13 18:03:30 +0530, 
@updated_at=2015-05-13 18:08:17 +0530, @status="0", @tagged=0, @merged_with_txaction_id=0, @note="NULL", @usd_amount=0.0, @attachment_ids="NULL", @automatch=0, @merchant_name="MERLINS", @tag_names=nil, @transfer_txaction_id=0, @fi_date_posted=2015-05-13 17:58:01 +0530, @transaction_status_id=0, @mc_code="NULL", @provider_txn_type="NULL", @latitude=19.09686, @longitude=72.8537753, @sync_status=31, @sec_account_id=0, @associated_sms_id=130672, @account_key="YzI5ZDUyNWY5NmYwNWFiNjJiYmE1YTk4Y2VkYTBjYTZmOGM5ZTI0NzE2MzU2MzAwMmU2OWU2MzNiYmQ2YTZhMA==", @id_on_client=229, @sync_delete_status=0, 
@matching_bill_id=0, @mapped_account_number=0, @additional_description="NULL", @category_id=45, @data_processing_flag=4, 
@derived_description="NULL", @final_description="NULL", @derived_merchant="Merlins Bar", @final_merchant="NULL", 
@tentative_location_flag=-1, @tentative_latitude="-90.00000000000", @tentative_longitude="-90.00000000000", @mongo_merchant_id="102", @parent_category_id=40, @raw_description="NULL", @formatted_description="NULL", @associated_sms_client_id=779, @account_client_id=4>

如果我尝试通过输入

来访问ID
print messages[0].id

它给了我一个错误

NoMethodError: undefined method `id' for #<SMSMessage:0x00000001681d88>

如何解决此问题?

PS - SMSMessage类代码

class SMSMessage
def initialize(id,account_id,upload_id,txaction_type_id,txid,finanalytics_txid,date_posted,sequence,sic_code,amount,raw_name,filtered_name,cleaned_name,merchant_id,memo,check_num,ref_num,balance,created_at,updated_at,status,tagged,merged_with_txaction_id,note,usd_amount,attachment_ids,automatch,merchant_name,tag_names,transfer_txaction_id,fi_date_posted,transaction_status_id,mc_code,provider_txn_type,latitude,longitude,sync_status,sec_account_id,associated_sms_id,account_key,id_on_client,sync_delete_status,matching_bill_id,mapped_account_number,additional_description,category_id,data_processing_flag,derived_description,final_description,derived_merchant,final_merchant,tentative_location_flag,tentative_latitude,tentative_longitude,mongo_merchant_id,parent_category_id,raw_description,formatted_description,associated_sms_client_id,account_client_id)
    @id = id.to_i
    @account_id = account_id.to_i
    @upload_id = upload_id.to_i
    @txaction_type_id = txaction_type_id.to_i
    @txid = txid.to_i
    @finanalytics_txid = finanalytics_txid.to_i
    @date_posted = Time.parse(date_posted)
    @sequence = sequence.to_i
    @sic_code = sic_code.to_i
    @amount = amount.to_f
    @raw_name = raw_name
    @filtered_name = filtered_name 
    @cleaned_name = cleaned_name
    @merchant_id = merchant_id
    @memo = memo
    @check_num = check_num
    @ref_num = ref_num
    @balance = balance.to_f
    @created_at = Time.parse(created_at)
    @updated_at = Time.parse(updated_at)
    @status = status
    @tagged = tagged.to_i
    @merged_with_txaction_id = merged_with_txaction_id.to_i
    @note = note
    @usd_amount = usd_amount.to_f
    @attachment_ids = attachment_ids 
    @automatch = automatch.to_i
    @merchant_name = merchant_name
    @tag_names = tag_names
    @transfer_txaction_id = transfer_txaction_id.to_i
    @fi_date_posted = Time.parse(fi_date_posted)
    @transaction_status_id = transaction_status_id.to_i
    @mc_code = mc_code
    @provider_txn_type = provider_txn_type 
    @latitude = latitude.to_f
    @longitude = longitude.to_f
    @sync_status = sync_status.to_i
    @sec_account_id = sec_account_id.to_i
    @associated_sms_id = associated_sms_id.to_i
    @account_key = account_key
    @id_on_client = id_on_client.to_i
    @sync_delete_status = sync_delete_status.to_i
    @matching_bill_id = matching_bill_id.to_i
    @mapped_account_number = mapped_account_number.to_i
    @additional_description = additional_description
    @category_id = category_id.to_i
    @data_processing_flag = data_processing_flag.to_i
    @derived_description = derived_description
    @final_description = final_description
    @derived_merchant = derived_merchant
    @final_merchant = final_merchant
    @tentative_location_flag = tentative_location_flag.to_i
    @tentative_latitude = tentative_latitude.to_f
    @tentative_longitude = tentative_longitude.to_f
    @mongo_merchant_id = mongo_merchant_id
    @parent_category_id = parent_category_id.to_i
    @raw_description = raw_description
    @formatted_description = formatted_description
    @associated_sms_client_id = associated_sms_client_id.to_i
    @account_client_id = account_client_id.to_i
end
end

1 个答案:

答案 0 :(得分:2)

由于您没有为对象属性定义访问器方法,因此应使用instance_variable_get

messages[0].instance_variable_get(:@id)

为了能够阅读id属性(即messages[0].id),您需要添加

attr_reader :id

到你的模特。如果您需要编写此属性,则应定义attr_writer :id.

对于(读和写)两者都有一个attr_accessor :id