Forum: Ruby ABS |ruby|

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
622fa8560c82dfaa59c91ec75efb0c19?d=identicon&s=25 Alex Combas (Guest)
on 2006-01-13 08:40
(Received via mailing list)
Quick question,

In math the absolute value is typically
the double pipe:
|x| = 27

This reminds me stuff like:
Stuff.each { |x| puts 'hello '+x }

Are these two related in any way?
I dont really see why they would be related but thought it was an
interesting coincidense nonetheless.
Af95bdaf87958c40150b813e94381bfd?d=identicon&s=25 Christer Nilsson (christer)
on 2006-01-13 10:22
Alex Combas wrote:
>
> In math the absolute value is typically
> the double pipe:
> |x| = 27
>
> This reminds me stuff like:
> Stuff.each { |x| puts 'hello '+x }
>
> Are these two related in any way?

No, this is just syntactic sugar. It's a placeholder for the iteration
variable. abs is a method in Numeric.

Christer
D52ab358f23657f81cf11b9d8f4fbb0d?d=identicon&s=25 ATOM (Guest)
on 2006-01-13 10:39
(Received via mailing list)
In math we also have the set-notation like:
A = { a | a > 0 }
which means something like: A is set of elements,
greater then 0... and so on. This could also
be read similar to block-syntax. We get a set of elements
on which we perform some operations (like map+lambda
call in funcional languages, anonymous function +
set of elements).
regards.
9a1a4c7f4da6961ef3f6503d7ff33a53?d=identicon&s=25 Adriano Ferreira (Guest)
on 2006-01-13 12:28
(Received via mailing list)
On 1/13/06, Alex Combas <alex.combas@gmail.com> wrote:
> In math the absolute value is typically
> the double pipe:
> |x| = 27
>
> This reminds me stuff like:
> Stuff.each { |x| puts 'hello '+x }
>
> Are these two related in any way?

IIRC, the block syntax in Ruby is reminiscent to Smalltalk syntax,
which used blocks like this:

[ :x | Transcript show: 'hello ', x ]      "the equivalent of Ruby
block { |x| puts 'hello '+x }"

and used | vars | to declare some temporary variables in a piece of
code.

| x y |
^ x + y

Square brackets are not square anymore (as they are used in Ruby for
indexing purposes - C inheritance). The odd variable names with
markers (:x) were replaced by the delimited block of names (| x |) and
voilĂ .
Regards,
Adriano.
This topic is locked and can not be replied to.