[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ABS |ruby|

Alex Combas

1/13/2006 7:34:00 AM

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.

--
Alex Combas
http://noodlejunkie.blo...


3 Answers

Christer Nilsson

1/13/2006 9:23:00 AM

0

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

--
Posted via http://www.ruby-....


ATOM

1/13/2006 9:37:00 AM

0

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.

Alex Combas wrote:
> Quick question,
>
> In math the absolute value is typically
> the double pipe:
> |x| = 27
>
> This reminds me stuff like:
> Stuff.each { |x| 'h puts ello '+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.
>
> --
> Alex Combas
> http://noodlejunkie.blo...

Adriano Ferreira

1/13/2006 11:27:00 AM

0

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.