<html> <head> <title>Ruby FAQ: Syntax </title> </head> <body> <a href="rubyfaq-3.html">Previous</a> <a href="rubyfaq-5.html">Next</a> <a href="rubyfaq.html#toc4">Table of contents</a> <hr> <h2><a name="s4">4. Syntax </a></h2> <h2><a name="ss4.1">4.1 What does <code>:exit</code> mean? </a></h2> 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. <br><br> "method_missing", "method_added" and "singleton_method_added" requires a symbol. <h2><a name="ss4.2">4.2 How can I access the value of a symbol? </a></h2> In the scope of "symbol", do "eval((:symbol).id2name)". <blockquote><code><pre> a = 'This is the content of "a"' b = eval(:a.id2name) a.id == b.id </pre></code></blockquote> <h2><a name="ss4.3">4.3 Is <code>loop</code> a control structure? </a></h2> It is a method defined in <code>Kernel</code>. The block which follows introduces a new scope for local variables. <h2><a name="ss4.4">4.4 <code>a +b</code> gives an error. </a></h2> It is parsed as <code>a(+b)</code>. Remove the space to the left of <code>+</code> or add a space to the right of <code>+</code>. <h2><a name="ss4.5">4.5 <code>s = "x"; puts s *10</code> gives an error. </a></h2> It is parsed as puts(s(*10)). Let the expression s*10 or s * 10. <h2><a name="ss4.6">4.6 Nothing appears with <code>p {}</code>. </a></h2> {} is parsed as a block, not a constructor of Hash. Let it <code>p({})</code>. <h2><a name="ss4.7">4.7 After <code>def pos= (val) print @pos,"\n"; @pos = val end</code>, I cannot use the method <code>pos = 1</code>. </a></h2> Methods with <code>=</code> appended are called in a receiver form. Invoke it as <code>self.pos = 1</code>. <h2><a name="ss4.8">4.8 What is the difference between <code>'\1'</code> and <code>'\\1'</code>? </a></h2> They are the same meaning. In a singlequote string, Only <code>\'</code> and <code>\\</code> are transformed and other combinations remain unchanged. <h2><a name="ss4.9">4.9 <code>p true or true and false</code> prints <code>true</code>, while <code>a=true if true or true and false</code> does not assign <code>true</code> to <code>a</code>. </a></h2> The first expression is parsed as <code>(p true) or true and false</code>. <code>and/or</code> is not parsed as an element of the argument of <code>p</code> but an element of an expression. <br><br> 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. <h2><a name="ss4.10">4.10 <code>p(nil || "")</code> returns "", while <code>p(nil or "")</code> gives parse error. </a></h2> || can conbine arguments. <code>or</code> combines only expressions, not arguments. Try <code>p nil or ""</code> and <code>p((nil or ""))</code>. <hr> <a href="rubyfaq-3.html">Previous</a> <a href="rubyfaq-5.html">Next</a> <a href="rubyfaq.html#toc4">Table of contents</a> </body> <html>