# Ruby expressions vs. values (like Perl?)

**URL:** <https://www.ruby-forum.com/t/ruby-expressions-vs-values-like-perl/72628>\
**Category:** Ruby\
**Created:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/ruby-expressions-vs-values-like-perl/72628 "2006-10-18T10:39:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/ruby-expressions-vs-values-like-perl/72628/1 "2006-10-18T10:39:29Z")

</div>

Does Ruby use the same terminology as Perl with regard to expressions  
versus values?

I read a great MJD comment on PerlMonks a while back (  
[http://www.perlmonks.org/?node\_id=72263](http://www.perlmonks.org/?node_id=72263) ). That comment notes how, in  
Perl, one needs to be well aware of the difference between expressions  
(the things you type into your code), and values (the things the  
interpreter evaluates expressions to).

It seems that, the big reason for watching the difference between  
expressions and values in Perl is because expressions turn into  
different values at runtime depending upon context.

So, back to Ruby, I believe that the rhs of the following statement is  
an “Array expression”:

irb(main):001:0\> foo = [‘a’,‘bee’,‘sea’]  
=\> [“a”, “bee”, “sea”]

but, what kind of expression is the rhs of the following:

irb(main):002:0\> bar = ‘dee’, ‘ee’, ‘eff’  
=\> [“dee”, “ee”, “eff”]

?

Incidentally, I also note that this one – although performing  
parallel assignment – also happens to be generating an Array value:

irb(main):003:0\> zz = ( a, b, c = ‘gee’, ‘aitch’, ‘aye’ )  
=\> [“gee”, “aitch”, “aye”]

irb(main):004:0\> zz  
=\> [“gee”, “aitch”, “aye”]

According to irb, the Array gets generated even if you leave off the  
“zz =” and the parentheses. Seems like extra work for the interpreter  
to go through (making that Array), just to do a parallel assignment,  
no?

Thanks,  
—John

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/ruby-expressions-vs-values-like-perl/72628/2 "2006-10-18T10:39:32Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----

In article  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
John G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> expressions and values in Perl is because expressions turn into  
> different values at runtime depending upon context.

This is what many people dislike about perl. In Ruby, the  
distinction mostly doesn’t exist. It’s an Object…

> =\> [“dee”, “ee”, “eff”]
> 
> ?

One of the nice things about Ruby is that you can always ask it  
what it thinks.

irb(main):090: bar.class  
=\> Array  
irb(main):010:0\> bar.methods  
=\> [“respond\_to?”, “index”, “select”, “\<\<”, “to\_a”, “delete\_if”,  
“slice”, “&”, “type”, “each\_index”, “length”,  
“protected\_methods”, “partition”, “sort!”, “assoc”, “to\_ary”,  
“eql?”, “\*”, “grep”, “instance\_variable\_set”, “+”, “is\_a?”,  
“hash”, “push”, “send”, “to\_s”, “-”, “rindex”, “reject”, “size”,  
“pack”, “join”, “class”, “reverse\_each”, “tainted?”, “collect!”,  
“private\_methods”, “rassoc”, “at”, “member?”, “ **send** ”,  
“compact!”, “untaint”, “|”, “find”, “each\_with\_index”, “reject!”,  
“flatten”, “id”, “pop”, “slice!”, “replace”, “collect”,  
“inspect”, “transpose”, “instance\_eval”, “reverse”, “all?”,  
“clone”, “entries”, “indexes”, “public\_methods”, “map!”, “uniq”,  
“fetch”, “extend”, “freeze”, “detect”, “values\_at”, “display”,  
“zip”, “ **id** ”, “shift”, “\<=\>”, “methods”, “method”, “map”,  
“==”, “clear”, “empty?”, “===”, “any?”, “indices”, “sort”,  
“nil?”, “dup”, “instance\_variables”, “include?”, “min”,  
“instance\_of?”, “flatten!”, “first”, “find\_all”, “delete\_at”,  
“nitems”, “each”, “object\_id”, “insert”, “reverse!”, “unshift”,  
“=~”, “inject”, “singleton\_methods”, “fill”, “delete”, “concat”,  
“uniq!”, “equal?”, “taint”, “sort\_by”, “instance\_variable\_get”,  
“max”, “[]”, “frozen?”, “compact”, “kind\_of?”, “last”, “[]=”]

> According to irb, the Array gets generated even if you leave off the  
> “zz =” and the parentheses. Seems like extra work for the interpreter  
> to go through (making that Array), just to do a parallel assignment,  
> no?

It’s not making an Array, it’s making a reference to an existing  
object. Every ruby statement generally returns a reference  
to the result, this can be an incredibly powerful tool.

\_ Booker C. Bense

-----BEGIN PGP SIGNATURE-----  
Version: 2.6.2

iQCVAwUBRS7BCGTWTAjn5N/lAQGrXgP+NWJ+xN04v1o67cQ9PSQeHCtakRJQAz3r  
Rx/ivNqUXXMeR9uL3G2Ffui8BTE1KGpnsci2rzBcrxn2w1yjqAOV/IkmKFrmk72g  
27FlxtqVCT3OonCx1KtsHF/8akrqlojFPwdw5MVvDXAxFQoYYOY2DE17WVmfXmY9  
T+wBUTromgw=  
=Ilxn  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/ruby-expressions-vs-values-like-perl/72628/3 "2006-10-18T10:39:38Z")

</div>

On 10/12/06, Booker C. Bense  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > but, what kind of expression is the rhs of the following:  
> > =\> Array  
> > irb(main):010:0\> bar.methods  
> > =\> [“respond\_to?”, “index”, “select”, “\<\<”, “to\_a”, “delete\_if”,  
> > “slice”, “&”, “type”, “each\_index”, “length”,  
> > [snip]

Right. I see that bar is an Array, but I what I meant was, what kind  
of expression is that comma-separated thingy on the right hand side  
(when I made the assignment to bar)? Evidently, the answer seems to be  
that ruby is allowing a little bit of extra syntactic sugar to make it  
so I don’t have to type those square brackets.

> > According to irb, the Array gets generated even if you leave off the  
> > “zz =” and the parentheses. Seems like extra work for the interpreter  
> > to go through (making that Array), just to do a parallel assignment,  
> > no?
> 
> It’s not making an Array, it’s making a reference to an existing  
> object.

Huh? irb seems to be clearly showing an Array. I’m thinking it’s like  
in C++ where you have this little short-lived temporary object. It  
seems like it’s an extra step for the interpreter to bother making  
that extra temporary – I’d think it could’ve just done the assignment  
to a, b, and c and be done with it.

—John

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [October 18, 2006, 10:41am UTC](https://www.ruby-forum.com/t/ruby-expressions-vs-values-like-perl/72628/4 "2006-10-18T10:41:04Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----

In article  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
John G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> of expression is that comma-separated thingy on the right hand side  
> (when I made the assignment to bar)? Evidently, the answer seems to be  
> that ruby is allowing a little bit of extra syntactic sugar to make it  
> so I don’t have to type those square brackets.

Yes. For good or ill, Ruby has various syntactic sugar bits  
sprinkled about.

> > object.
> 
> Huh? irb seems to be clearly showing an Array. I’m thinking it’s like  
> in C++ where you have this little short-lived temporary object. It  
> seems like it’s an extra step for the interpreter to bother making  
> that extra temporary – I’d think it could’ve just done the assignment  
> to a, b, and c and be done with it.

This is the part of Ruby that took me the longest to get my head  
around and I think causes lot’s of difficulties since it uses a  
reference model that’s quite different from many other languages.  
Variables are always a reference to an object, assignment copies  
the reference but not the object. In many languages you can do

a = b

and then manipulate a , but b will remain unchanged. In Ruby,  
both b and a are pointers to the underlying object and if you  
change a then b changes as well. There is only 1 Array object  
in the fragment above. In Ruby if you want a more conventional  
version of assignment you do

a = b.copy

\_ Booker C. Bense

-----BEGIN PGP SIGNATURE-----  
Version: 2.6.2

iQCVAwUBRS+f7GTWTAjn5N/lAQEjEgP+IWgJY5cyaJOb1Xp1I7t0kJulonMPuqKY  
Vj9wXYNgS05jKuGz0xRY8EVcMHopJxELn0+Zij0cknAYeEkSn+Balnnw3kJk+5oi  
Ao6w/9nK1eRZ4tOFcXBMqHyMedHlavKXBm+BIIJDJHH9COTJkFB/0lN/qRkCc7cE  
Ovaey9QPQhs=  
=vY74  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/ruby-expressions-vs-values-like-perl/72628/5 "2006-10-18T10:39:40Z")

</div>

John G. wrote:

> > > =\> [“a”, “bee”, “sea”]
> 
> that ruby is allowing a little bit of extra syntactic sugar to make it
> 
> > > =\> [“gee”, “aitch”, “aye”]  
> > > in C++ where you have this little short-lived temporary object. It  
> > > seems like it’s an extra step for the interpreter to bother making  
> > > that extra temporary – I’d think it could’ve just done the assignment  
> > > to a, b, and c and be done with it.
> 
> —John

Someone with better understanding of Ruby internals will need to  
confirm this because I may well be wrong, but I think you are correct.  
eval.c creates a temporary array out of the right-hand side (in the  
svalue\_to\_mrhs function I think). Page 91 of the Pickaxe (2nd Ed)  
explains this a little more. Whether or not optimising the Array away  
is possible or desirable is another question. Unless you are doing some  
pretty insane parallel assignment I wouldn’t have thought it makes any  
practical difference.

Alex G.

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [October 18, 2006, 10:44am UTC](https://www.ruby-forum.com/t/ruby-expressions-vs-values-like-perl/72628/6 "2006-10-18T10:44:02Z")

</div>

On 10/12/06, AlexG [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Someone with better understanding of Ruby internals will need to  
> confirm this because I may well be wrong, but I think you are correct.  
> eval.c creates a temporary array out of the right-hand side (in the  
> svalue\_to\_mrhs function I think). Page 91 of the Pickaxe (2nd Ed)  
> explains this a little more. Whether or not optimising the Array away  
> is possible or desirable is another question. Unless you are doing some  
> pretty insane parallel assignment I wouldn’t have thought it makes any  
> practical difference.

Yep, that’s exactly what seems to be happening.

One reason for this is so that a parallel assignment like

```
 x, y = y, x

```

works correctly.

If the interpreter simply broke that down into series of assignments  
without the temporary array, then it would need to detect such cases  
and handle them separately or:

```
x = 1
y = 2

```

# x, y = y, x

# converted into

```
x = y
y = x

```

x =\> 2  
y =\> 2

which is not what we want.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

IPMS/USA Region 12 Coordinator  
[http://ipmsr12.denhaven2.com/](http://ipmsr12.denhaven2.com/)

Visit the Project Mercury Wiki Site  
[http://www.mercuryspacecraft.com/](http://www.mercuryspacecraft.com/)

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [October 29, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/ruby-expressions-vs-values-like-perl/72628/7 "2006-10-29T15:28:30Z")

</div>

On 10/13/06, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> y = 2
> 
> ```
> 
> # x, y = y, x
> 
> # converted into
> 
> ```
> x = y
> y = x
> 
> ```
> 
> x =\> 2  
> y =\> 2
> 
> which is not what we want.

Ah. Of course. Thanks.

Also, to answer my own original question, I think actually that it may  
not’ve had much pith to it… That is to say, everything the  
interpreter sees is an “expression” of some kind (regardless of the  
name one wants to give that expression). I think, most of the time,  
ruby simply turns expressions into what values you think they should  
be turned into, and (as Booker mentions) sprinkles little syntactic  
sugar bits around here and there for the programmer’s convenience.

Perl does the same thing, except it has some arguably quirkier rules  
about the values it thinks the expressions should be evaluated to.

Thanks,  
—John
