Ruby Basics / define met;hod

  • Thread starter Chinna Karuppan
  • Start date
C

Chinna Karuppan

Hi,
I am new to Ruby and This is a very basic question.when I type p self in
irb it gives me 'main'.
when I say class what is happening in the backend.since within a class
end I am able to call define_method which is a private method and should
never be available to anybody and another intersting thing is it is not
accessible outside of the
class.
so my question is how is a private method accessible outside
what is happening when a class is defined. it is of type Class
automatically...I don't quite get it.
As soon as you start IRB a main object is created which is of type
Object.is it is right statement...
THnks
Chinna
 
T

Thomas Wieczorek

Hi,
I am new to Ruby and This is a very basic question.when I type p self in
irb it gives me 'main'.

I can't say much to the details, how irb is implemented, but you can
overwrite what "p object" returns, when you overwrite the inspect()
method:
class FooBar
end
p FooBar.new #=> #<FooBar:0x2aa50f8>

class FooBar
def inspect
"I am FooBar"
end
end
p FooBar.new #=> I am FooBar
when I say class what is happening in the backend.since within a class
end I am able to call define_method which is a private method and should
never be available to anybody and another intersting thing is it is not
accessible outside of the
class.

You can use the send method to call private methods in Ruby 1.8.6, I
am not sure if it works in 1.9
class Baz
private
def hello; "HELLO!"; end
end

Baz.new.send:)hello) #=> "HELLO!"
 
R

Robert Klemme

2008/3/6 said:
I am new to Ruby and This is a very basic question.when I type p self in
irb it gives me 'main'.
Correct.

when I say class what is happening in the backend.since within a class
end I am able to call define_method which is a private method and should
never be available to anybody and another intersting thing is it is not
accessible outside of the class.

irb(main):001:0> class Foo
irb(main):002:1> p [self,self.class]
irb(main):003:1> end
[Foo, Class]
=> nil
irb(main):004:0>

Inside a class body /self/ is the class instance being defined right now.
so my question is how is a private method accessible outside
what is happening when a class is defined.

First of all, inside "class...end" you are not "outside".

Second, if you want to access any private method you can do so via send:

irb(main):004:0> class Foo
irb(main):005:1> private
irb(main):006:1> def bar()123 end
irb(main):007:1> end
=> nil
irb(main):008:0> Foo.new.bar
NoMethodError: private method `bar' called for #<Foo:0x7ff82440>
from (irb):8
from :0
irb(main):009:0> Foo.new.send :bar
=> 123
it is of type Class
automatically...I don't quite get it.
As soon as you start IRB a main object is created which is of type
Object.is it is right statement...

I do not know what you mean by your last sentence. "main" is of type
Object - but it has some methods added:

$ irb
irb(main):001:0> puts class<<self;self;end.instance_methods(false).sort
bindings
cb
chws
conf
context
cws
cwws
exit
fg
help
include
install_alias_method
irb
irb_bindings
irb_cb
irb_change_binding
irb_change_workspace
irb_chws
irb_context
irb_current_working_binding
irb_current_working_workspace
irb_cwb
irb_cws
irb_cwws
irb_exit
irb_fg
irb_help
irb_jobs
irb_kill
irb_load
irb_pop_binding
irb_pop_workspace
irb_popb
irb_popws
irb_print_working_binding
irb_print_working_workspace
irb_push_binding
irb_push_workspace
irb_pushb
irb_pushws
irb_pwb
irb_pwws
irb_quit
irb_require
irb_source
irb_workspaces
jobs
kill
popb
popws
private
public
pushb
pushws
pwws
quit
source
to_s
workspaces
=> nil
irb(main):002:0>

Kind regards

robert
 
C

Chinna Karuppan

Thanks both of you...
When I say inside of class..end
class A
define_method:)name) { p self }
end
A.new.name will give me the self....if define_method is a private method
how is it I am able to access from inside the class ...end



THnks
ManickamPR.
 
R

Robert Klemme

2008/3/6 said:
Thanks both of you...
When I say inside of class..end
class A
define_method:)name) { p self }
end
A.new.name will give me the self....if define_method is a private method
how is it I am able to access from inside the class ...end

Please read my reply.

robert
 
C

Chinna Karuppan

I did read through your reply.especially this....
"Inside a class body /self/ is the class instance being defined right
now."
I still could not understand.
BTW even this is little foreign to me
puts class<<self;self;end.instance_methods(false).sort

is class an array that you are appending the self array and after that
you end.instance_methods
Can you explain it in a little black & white...
THnks
chinna
 
R

Robert Klemme

I did read through your reply.especially this....
"Inside a class body /self/ is the class instance being defined right
now."
I still could not understand.

You wondered why define_method can be invoked inside class...end because
it is private and should not be accessible "from outside". Private
methods can normally be only invoked without an explicit receiver, i.e.
only on /self/. I explained and demonstrated (via the printing) that
inside class...end /self/ is actually the class being defined. Hence
you can invoke define_method inside class..end. q.e.d.

Then I went on to explain that /private/ in Ruby is rather weak because
you can invoke private methods on any instance simply by going through
/send/.

Cheers

robert
 

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

Similar Threads

Ruby Basics 10
Basics of Require 5
Define method and def 7
JAXB2 Basics, toString generation 6
How to Read Ruby API / Documentation 12
STDIN 1
DRb Basics 19
How to define the assignment operator in Ruby ? 6

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top