New to Ruby, tutorial gives wrong example

I am just starting with Ruby with intention to learn and perhaps use it
to produce a GUI slightly different to RoR.

It’s interesting to note how many times one has to enter the same text.

Take this example, from troubleshooters.com #How OOP is Ruby.
Incidentally it does not work.- very frustrating for a beginner.

#!/usr/bin/ruby

class Rectangle
def initailize(y, x, height, width, linecolor, fillcolor, linewidth)
@y = y
@x = x
@height = height
@width = width
@linecolor = linecolor
@fillcolor = fillcolor
@linewidth = linewidth
end
attr_accessor :y, :x, :height, :width, :linecolor, :fillcolor,
:linewidth
end

rect = Rectangle.new(10, 20, 50, 60, “blue”,“yellow”)
rect.linewidth = 4
puts rect.x.to_s+", "+rect.fillcolor + ", "+rect.linewidth.to_s
-------============-------------

Just this required the same text 3 times, one to initialise, one to
declare variables and one for attr_accessor

Its just a simple tutorial app which produces error on line 16 wrong
number of arguments (6 for 0) ArgumentError. Not a helpful error and
Google is of little help.

I tried adding another value to the rect=Rectangle.new list above but
that does not work either.

It seems such a waste to type the same thing over and over, is there any
reason that a set of variables cannot be initialised, declared,
populated and attributed as above, in the same line? reducing typos.

Help in understanding is greatly appreciated
thank you in advance
Roger

You maxe a spelling mistake in the method name initialize.
"def initailize " → def initialize

saji

On Tue, Apr 10, 2012 at 3:18 PM, Roger McDonald
[email protected]wrote:

class Rectangle
:linewidth
Its just a simple tutorial app which produces error on line 16 wrong
Help in understanding is greatly appreciated
thank you in advance
Roger


Posted via http://www.ruby-forum.com/.

Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
email: [email protected]
url: http://www.u-aizu.ac.jp
bib: Web of Science

i just realized that i made (maxe) a spelling mistake too :wink:

On Tue, Apr 10, 2012 at 3:30 PM, Saji H. [email protected] wrote:

@x = x
rect = Rectangle.new(10, 20, 50, 60, “blue”,“yellow”)

University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
email: [email protected]
url: http://www.u-aizu.ac.jp
bib: Web of Science

Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
email: [email protected]
url: http://www.u-aizu.ac.jp
bib: Web of Science

You can use Struct for simple classes like this. The following should
work in the same way as your code:

Rectangle = Struct.new :y, :x, :height, :width, :linecolor,
:fillcolor, :linewidth # note the colons

rect = Rectangle.new 10, 20, 50, 60, “blue”,“yellow”, 99 # you need
to pass all arguments, linewidth too - it’s not optional
rect.linewidth = 4
puts rect.x.to_s+", "+rect.fillcolor + ", "+rect.linewidth.to_s

– Matma R.

It’s interesting to note how many times one has to enter the same text.

Take this example, from troubleshooters.com #How OOP is Ruby.
Incidentally it does not work.- very frustrating for a beginner.

It is not the fault of matz or the people here if you use examples
that have a typo in their “def initialize”.

You should learn ruby on your own rather than copy / paste examples
that never worked. But if these examples worked, then you should
avoid making typos.

Some beginners learn easy, some not. If you blame Ruby first rather
than the decision of the beginner in question then you make a mistake.

I can tell you how I learned ruby:

By writing ruby scripts. Many.

It is really the only way to get better in ruby.

Write as much as you can, solve as many problems as you can.

On Tue, Apr 10, 2012 at 8:55 AM, Bartosz Dziewoński
[email protected] wrote:

You can use Struct for simple classes like this. The following should
work in the same way as your code:

Rectangle = Struct.new :y, :x, :height, :width, :linecolor,
:fillcolor, :linewidth # note the colons

rect = Rectangle.new 10, 20, 50, 60, “blue”,“yellow”, 99 # you need
to pass all arguments, linewidth too - it’s not optional
rect.linewidth = 4

That’s not true: all values are optional.

irb(main):001:0> Rectangle = Struct.new :y, :x, :height, :width,
:linecolor, :fillcolor, :linewidth
=> Rectangle
irb(main):002:0> r = Rectangle.new
=> #
irb(main):003:0> r = Rectangle.new 10, 20, 50, 60, “blue”,“yellow”, 99
=> #<struct Rectangle y=10, x=20, height=50, width=60,
linecolor=“blue”, fillcolor=“yellow”, linewidth=99>
irb(main):004:0> r = Rectangle.new 10, 20, 50, 60, “blue”,“yellow”
=> #<struct Rectangle y=10, x=20, height=50, width=60,
linecolor=“blue”, fillcolor=“yellow”, linewidth=nil>
irb(main):005:0> r.linewidth = 4
=> 4
irb(main):006:0> r
=> #<struct Rectangle y=10, x=20, height=50, width=60,
linecolor=“blue”, fillcolor=“yellow”, linewidth=4>

One can also easily define methods with Struct:

irb(main):001:0> Rectangle = Struct.new :y, :x, :height, :width,
:linecolor, :fillcolor, :linewidth do
irb(main):002:1* def paint
irb(main):003:2> puts “[]”
irb(main):004:2> end
irb(main):005:1> end
=> Rectangle
irb(main):006:0> r = Rectangle.new 10, 20, 50, 60, “blue”,“yellow”
=> #<struct Rectangle y=10, x=20, height=50, width=60,
linecolor=“blue”, fillcolor=“yellow”, linewidth=nil>
irb(main):007:0> r.paint
[]
=> nil

Kind regards

robert

Wow, thank you all for replying, it is really appreciated.

It’s not the copy and paste that’s at fault,aside from my typo, it’s my
understanding of what error messages mean together with how to use them.
I intend to study more.
Thank you so much for your patience.

A question if I may please, and I am not sure I’m using the right
terminology here:
With the example above from karatedog, where a Class is created with
variables in a certain order, does that order and number of variables
have to be retained where ever the class is called, or, could one or
more variables be referenced say only the y, x,.
this is interesting, I must go and experiment
thank you all.

Roger McDonald wrote in post #1055979:

Wow, thank you all for replying, it is really appreciated.

You’re welcome!

A question if I may please, and I am not sure I’m using the right
terminology here:
With the example above from karatedog, where a Class is created with
variables in a certain order, does that order and number of variables
have to be retained where ever the class is called, or, could one or

You do not “call” classes, you only call (or “invoke”) methods.

more variables be referenced say only the y, x,.
this is interesting, I must go and experiment
thank you all.

The order in which you invoke setters (methods which end in “=”) is
completely at your own discretion.

Btw. if you are lazy and only need a data container you can also use
OpenStruct:

irb(main):005:0> require ‘ostruct’
=> true
irb(main):006:0> o=OpenStruct.new(a:100)
=> #
irb(main):007:0> o.a
=> 100
irb(main):008:0> o.b=77
=> 77
irb(main):009:0> o
=> #

Kind regards

robert

Roger McDonald wrote in post #1055746:

Just this required the same text 3 times, one to initialise, one to
declare variables and one for attr_accessor

It might seem awkward, but Ruby is such a language where:

  1. you cannot access an initiated class’ variables by default (there is
    a way, but that looks ugly).
  2. (almost) everything is a method

These concludes that if you want to access the variables of a class, you
have to write methods for that. That’s what ‘attr_accessor’ did, and it
did saved you a lot of typing :slight_smile:

As for the ‘initialize’, it is not necessary to initialize variables
during that phase, but then you would have to do that later, through the
methods that attr_accessor created.

class Rectangle
attr_accessor :y, :x, :height, :width, :linecolor, :fillcolor,
:linewidth
end

rect = Rectangle.new()
rect.y, rect.x, rect.height, rect.width, rect.linecolor, rect.fillcolor,
rect.linewidth = 10, 20, 50, 60, “blue”, “yellow”, 12

(you can rewrite your initialize method with multiple assignment as
above, it will be more compact and readable)

Robert K. wrote in post #1055994:

Btw. if you are lazy and only need a data container you can also use
OpenStruct:

What is the advantage of OpenStruct over Hash?
Thanks

W dniu 11 kwietnia 2012 17:54 użytkownik Földes László
[email protected] napisał:

Robert K. wrote in post #1055994:

Btw. if you are lazy and only need a data container you can also use
OpenStruct:

What is the advantage of OpenStruct over Hash?

Only the nicer syntax, nothing more.

– Matma R.