self

Hi!

self -> What is main?

How can I get the actual context(scope) where I'm in?

How do I get the object out of the object_id ?

thank you

Opti

Hi!

self -> What is main?

main is self inside the top binding. Basically it's an instance of an Object.

How can I get the actual context(scope) where I'm in?

you can get a binding of this scope by calling `binding` in this particular scope.

How do I get the object out of the object_id ?

ObjectSpace._id2ref(object_id)

Though I'm quite sure this may be a private API.

···

On 10/13/21 18:58, Die Optimisten wrote:

Hello!

Hi!

self -> What is main?

main is self inside the top binding. Basically it's an instance of an
Object.

What else? :slight_smile:

How can I get the actual context(scope) where I'm in?

you can get a binding of this scope by calling `binding` in this
particular scope.

I meant the name of that binding (like "object of class X", and its type
(method/proc/...)

( b.class == Binding ! )

Thank you!

···

Am 13.10.21 um 19:04 schrieb hmdne:

On 10/13/21 18:58, Die Optimisten wrote:

Hello!

Hi!

self -> What is main?

main is self inside the top binding. Basically it's an instance of an
Object.

What else? :slight_smile:

Not much more can be said about it, sadly. You can get it from any point in code by executing `TOPLEVEL_BINDING.receiver`

How can I get the actual context(scope) where I'm in?

you can get a binding of this scope by calling `binding` in this
particular scope.

I meant the name of that binding (like "object of class X", and its type
(method/proc/...)

( b.class == Binding ! )

Thank you!

Binding is not equivalent to "an object of class X". It refers to the state of the stack. Which contains local variable references and a `self` reference.

···

On 10/13/21 19:26, Die Optimisten wrote:

Am 13.10.21 um 19:04 schrieb hmdne:

On 10/13/21 18:58, Die Optimisten wrote:

When you start a Ruby process with ruby or irb command you find yourself in the Toplevel. But what the heck is Toplevel? Well, let's start exploration:

❯ irb
irb(main):001:0> self
=> main

Okay, we know that self returns current object, but what's main which class it belongs to? Let's explore:
irb(main):002:0> self.class
=> Object

So, Toplevel is an instance of Object class.
This is the reason why you can call puts and other Kernel module methods. It's because Kernel included in Object by default:
irb(main):003:0> Object.included_modules
=> [Kernel]

···

--
Sincerely, Evgeniy.

On Oct 13 2021, at 9:19 pm, hmdne <hmdne@airmail.cc> wrote:

On 10/13/21 19:26, Die Optimisten wrote:
> Hello!
> Am 13.10.21 um 19:04 schrieb hmdne:
>>
>> On 10/13/21 18:58, Die Optimisten wrote:
>>> Hi!
>>>
>>> self -> What is main?
>> main is self inside the top binding. Basically it's an instance of an
>> Object.
> What else? :slight_smile:
Not much more can be said about it, sadly. You can get it from any point
in code by executing `TOPLEVEL_BINDING.receiver`
>>>
>>> How can I get the actual context(scope) where I'm in?
>> you can get a binding of this scope by calling `binding` in this
>> particular scope.
>
> I meant the name of that binding (like "object of class X", and its type
> (method/proc/...)
>
> ( b.class == Binding ! )
>
> Thank you!
Binding is not equivalent to "an object of class X". It refers to the
state of the stack. Which contains local variable references and a
`self` reference.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>