# What is the equivalent of C++ "this"?

**URL:** <https://www.ruby-forum.com/t/what-is-the-equivalent-of-c-this/181770>\
**Category:** Ruby\
**Created:** [January 18, 2010, 2:50am UTC](https://www.ruby-forum.com/t/what-is-the-equivalent-of-c-this/181770 "2010-01-18T02:50:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [January 18, 2010, 2:50am UTC](https://www.ruby-forum.com/t/what-is-the-equivalent-of-c-this/181770/1 "2010-01-18T02:50:24Z")

</div>

I want to write a method within class Array that will put single  
quotes around every element of the array.

“this” doesn’t work in Ruby. If it did I would write

class Array  
def quote  
length.times {|i| this[i] = “’” + this[i].to\_s + “’”  
end  
end

Ruby doesn’t know what I mean when I say “this”. How do I call the []  
operator when I’m in the array class and have the language realize I’m  
not building a list?

---

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [January 18, 2010, 3:03am UTC](https://www.ruby-forum.com/t/what-is-the-equivalent-of-c-this/181770/2 "2010-01-18T03:03:27Z")

</div>

lalawawa wrote:

In Ruby, “this” is called self.

```
def quote
    length.times {|i| self[i] = "'" + self[i].to_s + "'"
end

```

A more idiomatic Ruby way to achieve the same thing would be use map or  
map!. The later method will replace each element of the array with the  
value produced in the block given.

def quote  
map!{|element| “’#{element}’”}  
end

Tor Erik

---

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [January 18, 2010, 3:17am UTC](https://www.ruby-forum.com/t/what-is-the-equivalent-of-c-this/181770/3 "2010-01-18T03:17:20Z")

</div>

On 2010-01-18T10:50:06, lalawawa wrote:

> Ruby doesn’t know what I mean when I say “this”. How do I call the []  
> operator when I’m in the array class and have the language realize I’m  
> not building a list?

self

/Allan
