Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Friday, December 18, 2009

Left Hand Values in Ruby

Local variables, Constants, Instance variables, Class variables and global variables

a = 1
A = 1
@a = 1
@@a = 1
$a = 1

Arefs and Attributes

a[:b] = 1
# a.[](:b, 1)

a.b = 1
a::b = 1

Combine

a, A, @a, @@a, $a, a[:b], a.b =
  1, 1, 1, 1, 1, 1, 1

Opassign

a += 1
# a, b += 1, 2  raises Syntax Error!

The only one syntax which allows to write expression in left hand sides is aref. For example, a[random(10)] = 1 is accepted. Also, I can write such codes:

alias []= instance_variable_set
alias [] instance_variable_get

a = Object.new
a['@a'] = 1
p a['@a'] #=> 1
p a #=> #<Object:0x40afb0 @a=1>

No comments:

Post a Comment

Followers