[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby Basics / define met;hod

Chinna Karuppan

3/6/2008 1:38:00 PM

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
--
Posted via http://www.ruby-....

7 Answers

Thomas Wieczorek

3/6/2008 2:13:00 PM

0

On Thu, Mar 6, 2008 at 2:37 PM, Chinna Karuppan
<chinnakaruppan@gmail.com> wrote:
> 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!"

Robert Klemme

3/6/2008 2:43:00 PM

0

2008/3/6, Chinna Karuppan <chinnakaruppan@gmail.com>:
> 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

--
use.inject do |as, often| as.you_can - without end

Chinna Karuppan

3/6/2008 3:07:00 PM

0

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.

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

Robert Klemme

3/6/2008 3:46:00 PM

0

2008/3/6, Chinna Karuppan <chinnakaruppan@gmail.com>:
> 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

--
use.inject do |as, often| as.you_can - without end

Chinna Karuppan

3/6/2008 4:19:00 PM

0

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

Robert Klemme wrote:
> 2008/3/6, Chinna Karuppan <chinnakaruppan@gmail.com>:
>> 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

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

S2

3/6/2008 4:55:00 PM

0

Chinna Karuppan wrote:

> 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

what he did is:

irb(main):005:0> a =
irb(main):006:0* class<<self
irb(main):007:1> self
irb(main):008:1> end
=> #<Class:#<Object:0xb7cf29a4>>

so now in a you have a class object. self is the current instance of the
class.
then with
a.instance_methods(false)
you can print all instance methods of self. the .sort only sorts them
alphabetically.

Robert Klemme

3/6/2008 5:46:00 PM

0

On 06.03.2008 17:18, Chinna Karuppan wrote:
> 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