# Why the resutls are different

**URL:** <https://www.ruby-forum.com/t/why-the-resutls-are-different/79641>\
**Category:** Ruby\
**Created:** [December 17, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/why-the-resutls-are-different/79641 "2006-12-17T16:16:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/why-the-resutls-are-different/79641/1 "2006-12-17T16:16:51Z")

</div>

Hi all,

I try to understand the concept of variable scopes. I define two classes  
with very similar structures. But Ruby complains class Y only. Any  
comments?

Thanks,

Li

class X  
def initialize(name,artist,duration)  
@name=name  
@artist=artist  
@duration=duration  
end  
puts “This is the name”" #{name}"  
end

test\_x=X.new(1,2,3)  
puts test\_x.inspect

> ruby variables1.rb  
> This is the name X  
> #\<X:0x28ae894 @artist=2, @name=1, @duration=3\>  
> Exit code: 0

class Y  
def initialize(arg1,arg2,arg3)  
@arg1=arg1  
@arg2=arg2  
@arg3=arg3  
end  
puts “This is arg1"” #{arg1}"  
end

```
test_y=Y.new(1,2,3)
puts test_y.inspect

```

> ruby variables2.rb  
> variables2.rb:15: undefined local variable or method `arg1’ for Y:Class  
> (NameError)  
> Exit code: 1

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/why-the-resutls-are-different/79641/2 "2006-12-17T16:32:21Z")

</div>

Li Chen wrote:

> puts test\_x.inspect
> 
> ```
> puts test_y.inspect
> 
> ```

Code in class definitions, such as your “puts” statement, is executed  
when the class is defined, in the context of the class being defined.  
Observe that in your first example, the value of “name” that is printed  
is not the value of the “name” argument to initialize. It is actually  
the name of the class. That is because the puts statement is in the  
context of the class and when you print #{name} you’re printing the  
result of calling the name method on the class object. You can simplify  
your example to this:

class X  
puts name  
end

Check out ri Module.name.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/why-the-resutls-are-different/79641/3 "2006-12-17T17:34:00Z")

</div>

Thank you all for the quick response.

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/why-the-resutls-are-different/79641/4 "2006-12-17T16:32:21Z")

</div>

From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf  
Of  
Li Chen  
Sent: Sunday, December 17, 2006 5:17 PM

> class X
> 
> @arg3=arg3
> 
> > Exit code: 1

In the first case, “name” is threated as “X.name” (method “name” of  
object  
“class X”).

V.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/why-the-resutls-are-different/79641/5 "2006-12-17T17:58:35Z")

</div>

Also, before you get into the habit of doing that (see the quote), it’s  
kind of considered bad form and Ruby automatically concatenating strings  
like that might not happen in future versions.

You can eliminate the double quotations so it is:  
puts “This is the name #{name}”

Or use direct concatenation with the two strings:  
puts “This is the name” + " #{name}"

But that would be silly when you can just move the space over and not  
have the whole #{} thing:  
puts "This is the name " + #{name}

Most people, I think, would use the first version.

Dan

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/why-the-resutls-are-different/79641/6 "2006-12-17T19:52:59Z")

</div>

Daniel F. wrote:

> You can eliminate the double quotations so it is:  
> puts “This is the name #{name}”
> 
> Or use direct concatenation with the two strings:  
> puts “This is the name” + " #{name}"
> 
> But that would be silly when you can just move the space over and not  
> have the whole #{} thing:  
> puts "This is the name " + #{name}

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/why-the-resutls-are-different/79641/7 "2006-12-17T20:29:01Z")

</div>

Hi –

On Mon, 18 Dec 2006, Daniel F. wrote:

> But that would be silly when you can just move the space over and not have  
> the whole #{} thing:  
> puts "This is the name " + #{name}

I think you mean:

puts "This is the name " + name

🙂 I agree that the first technique (all in one set of quotation  
marks) is usually best, both because it doesn’t create a new string  
for the addition, and because it won’t blow up if name isn’t a string.  
(Though maybe there are times when one would want it to, I suppose.)

David
