Ruby Basics / define met;hod

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-forum.com/.

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!"

···

On Thu, Mar 6, 2008 at 2:37 PM, Chinna Karuppan <chinnakaruppan@gmail.com> wrote:

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

···

2008/3/6, Chinna Karuppan <chinnakaruppan@gmail.com>:
        from :0

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

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-forum.com/.

Please read my reply.

robert

···

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

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

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-forum.com/\.

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

···

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.