<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Man With Code &#187; eval</title>
	<atom:link href="http://manwithcode.com/tag/eval/feed/" rel="self" type="application/rss+xml" />
	<link>http://manwithcode.com</link>
	<description>Teaching You, One Tutorial at a Time</description>
	<lastBuildDate>Tue, 22 Feb 2011 23:40:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Programming With Ruby Episode 17, Getting Advanced</title>
		<link>http://manwithcode.com/222/programming-with-ruby-episode-17-getting-advanced/</link>
		<comments>http://manwithcode.com/222/programming-with-ruby-episode-17-getting-advanced/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 08:56:27 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Ruby Programming]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[advanced]]></category>
		<category><![CDATA[bindings]]></category>
		<category><![CDATA[eval]]></category>
		<category><![CDATA[external]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[symbols]]></category>

		<guid isPermaLink="false">http://manwithcode.com/?p=222</guid>
		<description><![CDATA[Covered In This Episode: Symbols eval Bindings Running Other Programs Safe Levels Transcript: Hello Everybody and welcome to Programming With Ruby Episode 17, Getting Advanced. I&#8217;m Tyler, and this video is brought to you by manwithcode.com. By now, you know a large amount about Ruby, so in this episode we will be going over some [...]]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/0dnHp7Yhbuo&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/0dnHp7Yhbuo&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><strong>Covered In This Episode:</strong></p>
<ul>
<li>Symbols</li>
<li>eval</li>
<li>Bindings</li>
<li>Running Other Programs</li>
<li>Safe Levels</li>
</ul>
<p><strong>Transcript:</strong></p>
<p>Hello Everybody and welcome to Programming With Ruby Episode 17,<br />
Getting Advanced. I&#8217;m Tyler, and this video is brought to you by<br />
manwithcode.com.</p>
<p>By now, you know a large amount about Ruby, so in this episode we will<br />
be going over some advanced features that Ruby has.</p>
<p>More specifically I will be teaching you what Symbols are, and when to<br />
use them. I will be showing you how to use eval, and how to use<br />
bindings with eval. You will learn how to run other programs from<br />
Ruby. Finally I will show you what safe levels are.</p>
<p>Lets get started!</p>
<p><strong>Symbols</strong></p>
<p>Symbols are a type of variable that are very much like strings, but<br />
more lightweight. Symbols look like this:</p>
<pre class="brush: ruby; title: ; notranslate">
:variable
</pre>
<p>Symbols can&#8217;t be manipulated like strings can, which seems like a huge<br />
drawback, but they do have a couple benefits.</p>
<p>Each time you use a string, to say access a hash. Ruby creates an<br />
instance of that string. Where if you use a symbol, it is only ever<br />
instanced once. Meaning that the use of symbols will take up less<br />
memory than strings will, if you are, say accessing a hash many times.</p>
<p>Symbols are also slightly easier to type since the colon is on the<br />
home row on US keyboards.</p>
<p><strong>eval</strong></p>
<p>eval is a way of running Ruby code that is contained in a string. For<br />
example, lets say you have a string that looks like this:</p>
<pre class="brush: ruby; title: ; notranslate">
&quot;puts 'Hello World'&quot;
</pre>
<p>It is just a simple string, so it does nothing. But, if you use the<br />
method eval on that string it will execute the code inside. So this<br />
will print Hello World! on to the screen:</p>
<pre class="brush: ruby; title: ; notranslate">
eval &quot;puts 'Hello World!'&quot;
</pre>
<p>This isn&#8217;t always useful, but you can use it if you want your users to<br />
be able to enter Ruby code into your program.</p>
<p>You can also pass bindings to eval. So if we had this method</p>
<pre class="brush: ruby; title: ; notranslate">
def my_method my_binding
    eval &quot;puts x&quot;, my_binding
end

x = 5
my_method binding
</pre>
<p>This outputs:</p>
<pre class="brush: plain; title: ; notranslate">
5
</pre>
<p>Some of you may notice that the variable x isn&#8217;t defined in the method <code>my_method</code>. By using the binding method, we can make variable scopes portable!</p>
<p><strong>Running Other Programs</strong></p>
<p>There comes a time when you will want to be able to run a program from<br />
Ruby, maybe you want to automate something, or simply get the output<br />
from an external program.</p>
<p>There are a few ways of doing this.</p>
<p>The first is with the method exec, which runs an external programs,<br />
and quits the Ruby script at the same time:</p>
<pre class="brush: ruby; title: ; notranslate">
exec('ls') # dir on windows
# Program never gets here
</pre>
<p>There is also system, which does the same thing, but doesn&#8217;t quit the<br />
Ruby script, and returns true or false if the program was successful:</p>
<pre class="brush: ruby; title: ; notranslate">
system('ls') # dir on windows
# we do get this far
</pre>
<p>Finally we have the &#8220;back-tick&#8221; `. Which looks like a sideways single<br />
quote. On my keyboard it is above the tab key. You surround your<br />
command in the back-ticks, like you would for a sting. Unlike the other<br />
two methods of running a program, this method also returns the output<br />
of the program you run.</p>
<pre class="brush: ruby; title: ; notranslate">
variable = `ls`
</pre>
<p><strong>Safe Levels</strong></p>
<p>If you are running a Ruby interpreter online or in another environment<br />
where users can enter in and run Ruby code. They hold the ability to<br />
wreak havoc on your system.</p>
<p>The way to prevent this from happening is by using safe levels. Safe<br />
levels are a way of preventing the user from getting access to the<br />
file system, or changing any variables that the program has.</p>
<p>You set safe levels by setting the $SAFE variable. By default it is<br />
set to zero.</p>
<pre class="brush: ruby; title: ; notranslate">
$SAFE = 4
</pre>
<p>Ruby &#8220;taints&#8221; objects that could be dangerous.</p>
<p>There are five different safe levels.<br />
0 =&gt; The default, you can do anything<br />
1 =&gt; Can&#8217;t use environment variable, eval, load, require, and more.<br />
2 =&gt; Same as above and also can&#8217;t use files<br />
3 =&gt; All objects created are tainted, can&#8217;t be untainted<br />
4 =&gt; You can do almost nothing&#8230; Can&#8217;t modify the untainted, can&#8217;t<br />
use exit. Basically completely safe and sand-boxed.</p>
<p>That brings us to the end of the episode. If you liked these videos,<br />
please donate. It costs me in both money and time to make them.</p>
<p>If you have any questions, comments, or suggestions please don&#8217;t<br />
hesitate to leave a comment on this page or email me at<br />
tyler@manwithcode.com</p>
<p>Thanks for watching, goodbye!</p>
]]></content:encoded>
			<wfw:commentRss>http://manwithcode.com/222/programming-with-ruby-episode-17-getting-advanced/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<series:name><![CDATA[Ruby Programming]]></series:name>
	</item>
	</channel>
</rss>

