Exploring Everyday Things with R and Ruby: Discover the World Around You Through Programming

Exploring Everyday Things with R and Ruby: Discover the World Around You Through Programming

Sau Sheong Chang

Language: English

Pages: 260

ISBN: 2:00278297

Format: PDF / Kindle (mobi) / ePub

If you're curious about how things work, this fun and intriguing guide will help you find real answers to everyday problems. By using fundamental math and doing simple programming with the Ruby and R languages, you'll learn how to model a problem and work toward a solution.

All you need is a basic understanding of programming. After a quick introduction to Ruby and R, you'll explore a wide range of questions by learning how to assemble, process, simulate, and analyze the available data. You'll learn to see everyday things in a different perspective through simple programs and common sense logic. Once you finish this book, you can begin your own journey of exploration and discovery.

Here are some of the questions you'll explore:

*Determine how many restroom stalls can accommodate an office with 70 employees

drawing red stars at the position of the cursor. See Figure 1-3 for a sample run. Figure 1-3. Shoes doodler Wrap-up It’s impossible to stuff everything about Ruby into a single book, much less a single chapter. What I hope I’ve done here is introduce you to the language and provide you with a glimpse of what Ruby is able to do. I’ve skipped many of its more exciting features, including the much-talked-about metaprogramming capabilities. That topic could be and is a whole book on its own. I’ve

data? Tools, of course. They will be the subject of the next two chapters. These are not the only tools available to you, but they are the ones we will be using in this book. The two tools we will use are Ruby and R. I’ve chosen them for specific purposes. Ruby is easy to learn and to read, perfectly suited to explain concepts in human-readable code. I will be using Ruby to write simulations and to do preprocessing to get data. R, on the other hand, is great for analyzing data and for generating

parameter to these functions is the name of the file to write to. In this book, we will be creating PDF files. This is how we will create charts: pdf('some_file_name.pdf') # do some chart plotting dev.off()Plotting charts The workhorse of the graphics package is the plot() function. Calling plot() actually redirects it to the correct function according to the input parameters. Most of the time, however, if you call plot() with a set of numbers, it will create a scatterplot. Let’s take the

#{mail.subject}" puts $! end end end end The big difference here is in the implementation of write_row. Instead of the From and To email addresses, we get the body of the message as a string. We force the string to be encoded in UTF-8 in case it’s not (this is required by Ruby 1.9), then clean it up with the cleanup method. We also use Nokogiri, the XML parsing library, to parse through the HTML part of the message if it exists. The cleanup method runs through three sets of changes. The first

Example 7-4. Separate rule def separate distance = Vector[0,0] $roids.each do |roid| if nearby?(SEPARATION_RADIUS, roid) distance += self.position - roid.position end end @delta += distance/SEPARATION_ADJUSTMENT end For each roid in the system (this is where we use the $roid global variable where we kept all the roids in the system), we check whether it’s within the SEPARATION_RADIUS of our roid, and if it is, we move it away. The amount of distance to move is the current distance between our