Object.object.method()

  • Thread starter Johan Soderholm
  • Start date
J

Johan Soderholm

Hello, obviously it's a time for a noobie question. What the following
statement means?

Object.object.method()

Sample code:

window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
area = Gtk::DrawingArea.new()
area.window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::pENCIL))

I'm confused. Can anyone enlight me?

Thanks.
 
R

Rick DeNatale

Hello, obviously it's a time for a noobie question. What the following
statement means?

Object.object.method()
Sample code:

window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
area = Gtk::DrawingArea.new()
area.window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::pENCIL))


I assume you are talking about that last line.

area.window.set_cursor(dk::Cursor::pENCIL)

What that means is:

1. send the message window to the object referenced by the variable
area (which in the above code would be an instance of
Gtk::DrawingArea. This will return a result object.

2. send the result object which was returned in step 1 the message
set_cursor with the argument dk::Cursor::pENCIL



--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
J

Jean-Julien Fleck

2010/10/3 Johan Soderholm said:
Hello, obviously it's a time for a noobie question. What the following
statement means?

Object.object.method()

Sample code:

window =3D Gtk::Window.new(Gtk::Window::TOPLEVEL)
area =3D Gtk::DrawingArea.new()
area.window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::pENCIL))

I'm confused. Can anyone enlight me?

To complete Rick's answer, the "window" in the last line has
(normally) *nothing* to do with your window variable. Ruby sees that
you are trying to talk to the object "area" (because of the dot) so it
politely ask "area" if it has a method named "window" (which I assume
it has). You could try

fenetre =3D Gtk::Window.new(Gtk::Window::TOPLEVEL)
area =3D Gtk::DrawingArea.new()
area.window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::pENCIL))

it should still work (assuming you change all the other occurence of
*variable* "window" into "fenetre")

On the contrary, the code

fenetre =3D Gtk::Window.new(Gtk::Window::TOPLEVEL)
area =3D Gtk::DrawingArea.new()
area.fenetre.set_cursor(Gdk::Cursor.new(Gdk::Cursor::pENCIL))

should not work: ruby should complain with a "NoMethodError"

Cheers,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
B

Brian Candler

Rick said:
I assume you are talking about that last line.

area.window.set_cursor(dk::Cursor::pENCIL)

What that means is:

1. send the message window to the object referenced by the variable
area (which in the above code would be an instance of
Gtk::DrawingArea. This will return a result object.

2. send the result object which was returned in step 1 the message
set_cursor with the argument dk::Cursor::pENCIL

Or to put it another way, it's the same as this:

tmp = area.window
tmp.set_cursor(dk::Cursor::pENCIL)
 
R

Rick DeNatale

Alors! GTK ne parle pas fran=E7ais? <G>

And in Ruby 1.9 you could spell it properly as fen=EAtre, but it would
still cause a "No Method" error.

To complete Rick's answer, the "window" in the last line has
(normally) *nothing* to do with your window variable. Ruby sees that
you are trying to talk to the object "area" (because of the dot) so it
politely ask "area" if it has a method named "window" (which I assume
it has). You could try

fenetre =3D Gtk::Window.new(Gtk::Window::TOPLEVEL)
area =3D Gtk::DrawingArea.new()
area.window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::pENCIL))

it should still work (assuming you change all the other occurence of
*variable* "window" into "fenetre")

On the contrary, the code

fenetre =3D Gtk::Window.new(Gtk::Window::TOPLEVEL)
area =3D Gtk::DrawingArea.new()
area.fenetre.set_cursor(Gdk::Cursor.new(Gdk::Cursor::pENCIL))

should not work: ruby should complain with a "NoMethodError"

Cheers,



--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
J

Jean-Julien Fleck

2010/10/4 Rick DeNatale said:
Alors! GTK ne parle pas fran=E7ais? <G>

What a shame, isn't it ? ;o)

I almost wrote Fenster to avoid the use of =EA but for one my german is
not even close to what I was able to do in highshool (and I didn't do
much at that time...) and secondly I thought that the use of the
uppercase (although autorised when defining a method) would be even
more misleading on that matter.

Cheers !

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top