2

This is my first attempt with Ruby on Rails, so I apologize if the question is obvious, but unfortunately I can not find the answer anywhere.

class Client < Person

  clientList = []

  def initialize(name, surname, email, wallet)
    super(name, surname, email)
    @wallet = wallet
  end

  attr_reader :wallet, :clientList

  def to_s
    super  + ", #{@wallet}"
  end

  def add_to_array
    clientList + this.object 
    #i know its not correct
  end
end

I would like to create method, which allow me to add instances of client class to clientList array. What else is there later any option to use this method already in def initialize(name, surname, email, wallet). Something like this.add_to_array

I would like to have array with all clients inside, but i don't want to use method add_to_array everytime i create new client. It should be automatic.

2
  • I suggest you to read Class and Instance Variables In Ruby Commented Nov 3, 2016 at 13:50
  • @Зелёный i see, so first of all clientList should be @@clientList, as its class varible, not just one object, right? Commented Nov 3, 2016 at 13:53

2 Answers 2

1

To add client instances to clientList you have to change clientList to be at least class instance variable, add accessor for it and a call add_to_array (I'd rename it to add_to_clients_list) to initialize method, so that the clientList updates everytime the Client instance is created:

class Client < Person

  @clientList = []

  class << self
    attr_accessor :clientList # add an accessor, so you can use Client.clientList
  end

  def initialize(name, surname, email, wallet)
    super(name, surname, email)
    @wallet = wallet
    add_to_array # call a method, that adds this created instance to array
  end

  attr_reader :wallet, :clientList

  def to_s
    super  + ", #{@wallet}"
  end

  def add_to_array
    self.class.clientList << self 
  end
end

Now:

# create instance of Client
Client.new(1,2,3,4)
#<Client:0x007f873db25a68 @wallet=4>
# It is automatically added to `clientList`
Client.clientList
#=> [#<Client:0x007f873db25a68 @wallet=4>]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! I couldn't find anywhere article, which is explaining how to use in Ruby something like this in Java. I will read more about self for sure.
@DanZawadzki yea, self is super-super-super important concept in Ruby. Understanding it almost guarantees you understand most of Ruby :) Btw, if that worked for you, you can accept the answer (look for a checkmark near the answer score)
@AndreyDinenko Ready! I really really appreciate that you wanted to devote your time to help me! Thanks a lot!
@DanZawadzki no probs at all :)
0

You could try this

class Client < Person
  @@clientList = []

  def initialize(name, surname, email, wallet)
    super(name, surname, email)
    @wallet = wallet
    @@clientList << self
  end

  attr_reader :wallet, :clientList

  def to_s
    super  + ", #{@wallet}"
  end
end

But to be honest I would suggest that you restructure this code. If you are using are rails this is something the would be much better handled by using active record models with has many associations.

class ClientList < ActiveRecord::Base
  has_many :clients
end

class Person < ActiveRecord::Base
end

class Client < Person
  belongs_to :client_list
end

6 Comments

Thanks a lot or your help! But it looks like i will have new clientList everytime i create client class object, or am i wrong?
this it not what OP wants - I would like to have array with all clients inside, but i don't want to use method add_to_array everytime i create new client. It should be automatic. :)
Andrey where did you come from I have seen you answer anything all week now all of a sudden your back. Yea you are right, I updated.
@CdotStrifeVII I've been to Tel Aviv, surfing and stuff - no internet and sea, pleasure someone noticed my absence :)
btw, with this edit there is no need in add_to_array method at all :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.