Sophie

Sophie

distrib > CentOS > 5 > x86_64 > by-pkgid > 6d36cb72372cfb7c8fee63f4d6dc0530 > files > 148

ruby-docs-1.8.5-31.el5_9.x86_64.rpm

<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.  &quot;exit&quot;.intern gives the same integer.
&quot;catch&quot;, &quot;throw&quot;, &quot;autoload&quot;, etc, require a string or a symbol as
an argument. 
<br><br>
&quot;method_missing&quot;, &quot;method_added&quot; and &quot;singleton_method_added&quot;
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 &quot;symbol&quot;, do &quot;eval((:symbol).id2name)&quot;.

<blockquote><code><pre>
a = 'This is the content of &quot;a&quot;'
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 = &quot;x&quot;; 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,&quot;\n&quot;; @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).
&quot;if&quot; has a lower priority than &quot;and/or&quot;.  &quot;and&quot; and &quot;or&quot; has the
same priority and parsed from left to right.

<h2><a name="ss4.10">4.10 
<code>p(nil || &quot;&quot;)</code> returns &quot;&quot;, while <code>p(nil or &quot;&quot;)</code>
gives parse error.
</a></h2>


|| can conbine arguments.  <code>or</code> combines only expressions,
not arguments.
Try <code>p nil or &quot;&quot;</code> and <code>p((nil or &quot;&quot;))</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>