Statement vs expression

In "The Ruby Way" (2nd) Hal Fulton states "Ruby is an expression-oriented language. Why use a statement when an expression will do?"

I though statements and expressions were the same, that is "a = b + c". Pointer to an explanation?

Thanks!

Leam

Basically ruby's commitment to the idea means you can do things like "a =
if b then c else d end". In other languages "if" is a statement, with no
return value. In ruby just about everything (if, unless, while, begin,
case) is an expression that will return the value of the last expression
contained inside it. You'll often see, say, a ruby method defined with just
an "if" expression and no apparent return statements. In fact it's
returning the result of whichever branch is selected.

Even class, method and module definitions can act as expressions for
certain metaprogramming techniques.

Hope that's helpful.

···

On Wed, 23 Sep 2015 at 13:40 Leam Hall <leamhall@gmail.com> wrote:

In "The Ruby Way" (2nd) Hal Fulton states "Ruby is an
expression-oriented language. Why use a statement when an expression
will do?"

I though statements and expressions were the same, that is "a = b + c".
Pointer to an explanation?

Thanks!

Leam

Expressions evaluate to a value, statements don't. In Ruby everything
tends to be an expression, so everywhere you need to write a value,
you can write an expression instead. In other languages there are
statements, which don't return a value and can't be written in places
where the language is expecting a value. For example, control flow
statements (if, while, etc) do not return a value in many languages,
while in Ruby they do:

2.2.1 :011 > def a value
2.2.1 :012?> puts "method a got: #{value}"
2.2.1 :013?> end
=> :a
2.2.1 :014 > a(if 3 < 2 then "asdf"; else "qwer"; end)
method a got: qwer

Jesus.

···

On Wed, Sep 23, 2015 at 1:39 PM, Leam Hall <leamhall@gmail.com> wrote:

In "The Ruby Way" (2nd) Hal Fulton states "Ruby is an expression-oriented
language. Why use a statement when an expression will do?"

I though statements and expressions were the same, that is "a = b + c".
Pointer to an explanation?