Ruby: Hash, Arrays and Objects for storage information -
i learning ruby, reading few books, tutorials, foruns , one... so, brand new this. trying develop stock system can learn doing. questions following:
i created following store transactions: (just few parts of code)
transactions.push type: "buy", date: date.strptime(date.to_s, '%d/%m/%y'), quantity: quantity, price: price.to_money(:brl), fees: fees.to_money(:brl)
and 1 colleague here suggested create transaction class store this. so, next storage information had, did:
@dividends_from_stock << dividendsfromstock.new(row["approved"], row["value"], row["type"], row["last day with"], row["payment day"])
now, first question: way better? hash in array or object in array? , why?
this @dividends_from_stock returned method 'dividends'.
i want find dividends paid above specific date:
puts ciel3.dividends.find_all {|dividend| date.parse(dividend.last_day_with) > date.parse('12/05/2014')}
i following:
#<dividendsfromstock:0x2785e60> #<dividendsfromstock:0x2785410> #<dividendsfromstock:0x2784a68> #<dividendsfromstock:0x27840c0> #<dividendsfromstock:0x1ec91f8> #<dividendsfromstock:0x2797ce0> #<dividendsfromstock:0x2797338> #<dividendsfromstock:0x2796990>
ok able spot (i think) objects has date higher 12/05/2014. (second question) how can information regarding 'value' (or other information) stored inside objects?
- generally better define classes. classes have names. understand going on when program gets big. can see class of each variable this:
var.class
. if use hashes everywhere, confused because these calls returnhash
. if define classes things, see class names. - define methods in classes return information need. if define method called
to_s
, ruby call behind scenes on object when print or use in interpolation (puts "some #{var} here"
).
Comments
Post a Comment