New to Ruby some problems

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 :slight_smile:

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 :slight_smile:

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
@array[index]
end

def []=(index, value)
@array[index] = value
end

Hope this helps. Welcome to Ruby!

cr

On Aug 22, 2011, at 8:53 AM, Chuck R. wrote:

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.
@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

I am sorry I can’t fix the errors though :S:S!

With Ruby, you should try to aim for terse and simple code.

This here:

def array
return @array
end

Can be rewritten as:

attr_reader :array

In class MATRIX.

Also you can omit ‘return’. I use that especially for short methods.
(In longer methods I sometimes use it for an early return or to make
code more explicit to my eyes.)

On Mon, Aug 22, 2011 at 3:05 PM, Chuck R. [email protected]
wrote:

@array[index(x, y)] = value
end

def get(x, y)
@array[index(x,y)]
end

[…]
end

Not sure if you know this, but you can actually still use the []
approach
with multiple arguments, to avoid “set” and “get” method names. A stupid
example:

class X
def []=(a, b, c)
p [a, b]
@a, @b = c, c.reverse
end

attr_reader :a, :b
end

x = X.new
x[1, 2] = “foo” #=> [1, 2]; “foo”
p x #=> #<X:0x00000100861f18 @a=“foo”, @b=“oof”>
puts x.a #=> foo
puts x.b #=> oof

Live output: http://ideone.com/UxWNS

On Aug 22, 2011, at 10:38 AM, Adam P. wrote:

Not sure if you know this, but you can actually still use the [] approach
with multiple arguments, to avoid “set” and “get” method names. A stupid
example:

I did not know that! Thanks for telling me.

cr

On Aug 22, 2011, at 10:56 AM, jack jones wrote:

I tried to read it but I couldn’t understand a word I am sorry

Jack, please ask some specific questions. We are all more than willing
to help.

You already have a set of code that you are trying to run. The code is
riddled with syntax errors that the runtime is highlighting for you. We
aren’t going to fix up every last detail for you; as a C++ coder you are
probably acclimated to handling much weirder errors and fixing them. The
errors given by the Ruby runtime should be more than sufficient for you
to fix them.

With those fixes and some of the suggestions given in this thread, you
should be much further along. What’s next?

cr

I tried to read it but I couldn’t understand a word I am sorry

On Mon, Aug 22, 2011 at 11:26 PM, jack jones [email protected]
wrote:

I am sorry I can’t fix the errors though :S:S!

Attachments:
http://www.ruby-forum.com/attachment/6538/2Dto1D.rb

when in ruby, think in ruby.
when new in language, start slowly, a line of code at a time, rtfm.

try eg,

$ cat -n test_matrix.rb
1 # This is a program to store a two dimension array into one
dimension only … ROW major
2 class MATRIX
3 attr_reader :array
4 def initialize(rows, cols)
5 @cols = cols
6 @array = Array.new(rows*cols){ rand(10)}
7 end
8
9 # set desired row and column and value
10 def []=(row, column, value)
11 position = index row, column
12 @array[position] = value
13 end
14
15 # retrieve value from row, column
16 def [](row, column)
17 position = index row, column
18 return @array[position]
19 end
20
21 private
22 def index row, column
23 return (row-1) * @cols + (column-1) # ruby array index
starts at 0
24 end
25 end
26
27
28 mat = MATRIX.new(2,2)
29 puts “before”
30 p mat.array
31
32 puts “after”
33 mat[1,1]=11
34 mat[1,2]=12
35 mat[2,1]=21
36 mat[2,2]=22
37
38 puts mat[1,1]
39 puts mat[1,2]
40 puts mat[2,1]
41 puts mat[2,2]
42
43 p mat.array
44
45

$ ruby test_matrix.rb
before
[5, 8, 6, 4]
after
11
12
21
22
[11, 12, 21, 22]

kind regards -botp

Thank you very much Chuck R. :slight_smile:
The problem is I don’t understand those functions :

now name just [] = ?? The problem is that I don’t get the magic

10    def []=(row, column, value) # how does it works?
11      position = index row, column
12      @array[position] = value
13    end
15    # retrieve value from row, column
16    def [](row, column)
17      position = index row, column
18      return @array[position]
19    end

You know operators and functions in C++? In Ruby these two are both
defined in the same way, using def (and, in fact, are the same thing).

– Matma R.

jack jones wrote in post #1018220:

Thank you very much Chuck R. :slight_smile:
The problem is I don’t understand those functions :

now name just [] = ?? The problem is that I don’t get the magic

10    def []=(row, column, value) # how does it works?
11      position = index row, column
12      @array[position] = value
13    end
15    # retrieve value from row, column
16    def [](row, column)
17      position = index row, column
18      return @array[position]
19    end

‘[]=’ is actually a method. Does that help?

Jeff M. wrote in post #1018257:

jack jones wrote in post #1018220:

Thank you very much Chuck R. :slight_smile:
The problem is I don’t understand those functions :

now name just [] = ?? The problem is that I don’t get the magic

10    def []=(row, column, value) # how does it works?
11      position = index row, column
12      @array[position] = value
13    end
15    # retrieve value from row, column
16    def [](row, column)
17      position = index row, column
18      return @array[position]
19    end

‘[]=’ is actually a method. Does that help?

More precisely, ‘[]=’ is the name of the method. You can name a method
with three letters, right?

def abc(str)
puts str
end

This is the same:

def []=(str)
puts str
end

The name of the first method is ‘abc’, and the name of the second method
is ‘[]=’. Here is an example:

class Dog
def abc(str)
puts str
end

def []=(str)
puts str
end
end

d = Dog.new
d.abc(‘hello’)
d.[]=(‘world’)

–output:–
hello
world

So in ruby, the []= method is just a method with a funny looking name
that is hard to pronounce. However, in the case of []= ruby lets you do
some special things; you can call []= like this:

d[val1] = val2

and ruby translates that into:

d.[]=(val1, val2)

Here is a more complete example:

class Dog
attr_reader :attributes

def initialize
@attributes = {}
end

def abc(str)
puts str
end

def []=(key, val)
@attributes[key] = val
end
end

d = Dog.new
d.abc(‘hello’)
d[‘greeting’] = ‘world’

p d.attributes

–output:–
hello
{“greeting”=>“world”}

However, ruby does some tricky things with the second method, so you
can call it like this:

First of all thank you very much 7stud :slight_smile:
okay I got the method but I had a question how would Ruby know what’s in
the [ ] and what’s out of them
for example :

class Dog
@array = []
def []=(arg1, arg2, arg3)
array[arg1 + arg2] = arg3 # any operation
end
end

d = Dog.new
d.abc(‘hello’)
d.[1,2]= 3

or

d.[1] = 2, 3

we have 3 args where to put each??

another thing what if I have more than one array

I’ll have to define the method name as def array1[]= (arg1, arg2,

arg3)

that would be right or wrong??

On Aug 25, 2011, at 7:34 AM, jack jones wrote:

end
arg3)

that would be right or wrong??

I don’t think we can teach you the entire language on this mailing list.

I recommend you take a look at a few books.

These are very good but cost a few $$.

http://amzn.to/qWRLUg

http://amzn.to/r5BdHQ

Here’s a free one but it’s a tad out of date (really only covers Ruby
1.8).

http://mislav.uniqpath.com/poignant-guide/

Work through the examples. When you get stuck or confused, drop a note
here and we’ll try to help.

cr

[]= operator can take either two three arguments - they’re written
like this: array[a, b] = c or array[a] = b (this is the same as
array.[]=(a, b, c) or array.[]=(a, b) ).

See the docs for this method to see why are the three arguments
needed: class Array - RDoc Documentation

– Matma R.

2011/8/25 Bartosz Dziewoński [email protected]

[]= operator can take either two three arguments

Not quite true. :slight_smile:

class X
def []=(a, b, c, d)
p [a, b, c, d]
end
end

x = X.new
x[1, 2, 3] = 4

#=> [1, 2, 3, 4]

thank you all very much :slight_smile:

On Thu, Aug 25, 2011 at 4:57 PM, jack jones [email protected]
wrote:

thank you all very much :slight_smile:

I just wanted to point out something that I think hasn’t been
mentioned. In the “assignment-like” methods, where Ruby applies
syntactic sugar so that you can use the usual syntax, Ruby also does
something that is not the same as with other method calls: the result
of the “assignment” expression is the expression’s right hand side,
not the result value of the method:

ruby-1.8.7-p334 :001 > class A
ruby-1.8.7-p334 :002?> def []= a,b
ruby-1.8.7-p334 :003?> @value = a + b
ruby-1.8.7-p334 :004?> @value
ruby-1.8.7-p334 :005?> end
ruby-1.8.7-p334 :006?> end
=> nil
ruby-1.8.7-p334 :007 > a = A.new
=> #<A:0xb70bd4cc>
ruby-1.8.7-p334 :008 > a[3] = 4
=> 4
ruby-1.8.7-p334 :009 > a
=> #<A:0xb70bd4cc @value=7>

Note that the value of the expression in line 8 is 4 (as shown by IRB)
and not 7, which is the value returned by the method []=
If you use the non-syntactic sugar version, though, the return value
is whatever the method returns:

ruby-1.8.7-p334 :010 > a.[]=(3,4)
=> 7

This is usually not a problem, since for this to bite you, you need to
be doing something in the []= and trying to use that return value
outside, not very likely. But anyway, I thought it was worth
mentioning.

Jesus.