From: Chuck Remes Date: 2011-08-22T23:05:57+09:00 Subject: Re: New to Ruby some problems On Aug 22, 2011, at 8:53 AM, Chuck Remes wrote: > On Aug 22, 2011, at 8:32 AM, jack jones wrote: > >> I am new to Ruby, My mother tongue is C++ .. I have too many problems I >> hope you'd help me with that in the 2 short attached files .. thank you >> very much :) >> >> Attachments: >> http://www.ruby-forum.com/attachment/6536/2Dto1D.rb >> http://www.ruby-forum.com/attachment/6537/Complex.rb > > First, fix the syntax errors. You are missing commas in some of your method definitions and adding spaces after the '@' character in your instance variables. There may be other issues, but the Ruby runtime should point them out to you when you try to load and execute your code. > > To answer your question about adding a setter/getter for the MATRIX class, you could define #[] and #[]= for that purpose to delegate to your @array ivar. > > e.g. > > def [](index) > @array[index] > end > > def []=(index, value) > @array[index] = value > end Sorry, I didn't read the problem closely enough (too early in the morning!). You need to pass a ROW and COLUMN to set or get a value in your matrix instead of just a single value like I posted above. Something like this might be a good start (untested). class Matrix def initialize(rows, cols) @array = [] @rows, @cols = rows, cols end def set(x, y, value) @array[index(x, y)] = value end def get(x, y) @array[index(x,y)] end private def index(x, y) (x * @cols) + y end end