Previous Next Table of contents

4. Syntax

4.1 What does :exit mean?

It is an integer(Fixnum) called a symbol which corresponds one to one with the identifier. "exit".intern gives the same integer. "catch", "throw", "autoload", etc, require a string or a symbol as an argument.

"method_missing", "method_added" and "singleton_method_added" requires a symbol.

4.2 How can I access the value of a symbol?

In the scope of "symbol", do "eval((:symbol).id2name)".
a = 'This is the content of "a"'
b = eval(:a.id2name)
a.id == b.id

4.3 Is loop a control structure?

It is a method defined in Kernel. The block which follows introduces a new scope for local variables.

4.4 a +b gives an error.

It is parsed as a(+b). Remove the space to the left of + or add a space to the right of +.

4.5 s = "x"; puts s *10 gives an error.

It is parsed as puts(s(*10)). Let the expression s*10 or s * 10.

4.6 Nothing appears with p {}.

{} is parsed as a block, not a constructor of Hash. Let it p({}).

4.7 After def pos= (val) print @pos,"\n"; @pos = val end, I cannot use the method pos = 1.

Methods with = appended are called in a receiver form. Invoke it as self.pos = 1.

4.8 What is the difference between '\1' and '\\1'?

They are the same meaning. In a singlequote string, Only \' and \\ are transformed and other combinations remain unchanged.

4.9 p true or true and false prints true, while a=true if true or true and false does not assign true to a.

The first expression is parsed as (p true) or true and false. and/or is not parsed as an element of the argument of p but an element of an expression.

The second is parsed as a=true if (true or true and false). "if" has a lower priority than "and/or". "and" and "or" has the same priority and parsed from left to right.

4.10 p(nil || "") returns "", while p(nil or "") gives parse error.

|| can conbine arguments. or combines only expressions, not arguments. Try p nil or "" and p((nil or "")).
Previous Next Table of contents