Skip to main content

Posts

Showing posts with the label Ruby

Programming is a Craft (And that's a good thing)

Programming is a craft. You are building something. The modern languages have given the programmer great ownership of the craft. Before it was more like an assembly and one programmer was responsible for one cog in a big machine and few programmers, the elite, saw the whole machine, understood the whole craft. But now with languages like Ruby and Python, the massive power at the hands of the programmer bring the whole thing back to a craft. It is something you do with your hands. It is creative, like writing and while not physical or tactile, it is built piece by piece. The connection is deep and actually obscured by the way programming is taught. Because programming originated from a mathematical discipline and math has mostly lost its craft origins and now is taught more as an intellectual pursuit, it is often forgotten that math is but a tool itself. Not an end goal. Math is used to do things with it. Calculous to measure trajectories of objects. Geometry is essential for architec...

Great Technical Books on Programming

We are in the middle of a computer programming languages explosion. In the last few years, a slew of programming languages have come into their own, others have been revived with new expressions and a whole bunch of them have been born anew. Also a new modality has appeared of the polyglot programmer, that is a programmer that works in multiple languages. This last change is the most significant. Because for a long time programming was dominated by a very few select group of languages: C, C++, Java and C#. All of which are related. And dominated by a tool set like Visual Studio with C# and Visual Basic. Now that is no longer the case and things are for the better. Recently I taught myself how to program and want to highlight a few books that are good reads. Most books in programming fall into a trap, that is they don't teach programming at all, but instead teach only the programming language. That's like teaching someone the rules to America Football and expecting them to a...

Ruby Idioms part 1

Ruby is like a language in that it has phrases that are used, and ones that work better than others. The most common rubyism is a ||= b where a is set to b if it is not nil , false or undefined . It basically means if a  has a value move on but if it doesn't set it to b . Simple enough. But an even more powerful one but less well known is the index feature one. Arrays, hashes and even more importantly strings have this function too. In hashes it's the normal way to get a value by sending it its key. So: hashy = {:key1 =>  :value1, :key2 => {:subkey1 => :subvalue1}} doing hashy[:key1] will spit out :value1  But it's really useful for strings too. stringy = "Friends, Romans, countrymen, lend me your ears;" stringy['lend me your ears'] #=> "lend me your ears" You can also chain this calls which leads to some weird things. stringy['lend me your ears'].upcase['EARS']   #=>  "EARS" I just u...

How to pass a Rails or Sinatra variable into JQuery.

A few days back I got a code challenge for a cool job position. For whatever reason the test was very light on Rails very heavy on JQuery, which I didn't know much about frankly. So in less than 24 hours I had to not only learn enough of it but also make it work into a modal window widget. I got it done, but it wasn't elegant at all. I clobbered it even if it worked. The first challenge was to get information into webpage from a Rails application, I chose JSON and thought it'd be easy-peasy, but transferring a JSON object is a cross-domain script violation and most browsers won't let it pass. So I learned about JSONp which wraps the JSON object in a JQuery call-back. Lacking the easy Ruby ways of determining what things are, at first I'd no idea what the JSONP object was and just had my Rails app send JSON. Once I figured what it was, it was trivial to get Rails to do it. All I had to do was add: :callback => params[:callback]  to the format property in the con...

Advantages of Different Ruby Implementations

Some one asked me what where the advantages of JRuby lately. I had to think and that got me to this post. JRuby - Runs on Android - Can be uses to pre-comple code to Java Byte code for speed and code obscurity. :thumbs up: - Runs on a Java-Virtual-Machine server. - Runs on any computer w/ Java (whether it has Xcode or not) MacRuby - Scottish. - GUI apps that use native cocoa frame work. - iPhone apps through Ruby Motion. mRuby This is 'embedded ruby' and it hasn't been released yet but from what I know: - Very light, requires little memory and space to install. - Gives you Ruby expressiveness where a program language like Lua could only go before. IronRuby Sadly semi-abandoned. This Ruby's goal was to compile on Microsoft's .Net framework. - All those poor .Net developers doomed to MS's C++++ (C#) would be able to taste salvation in Ruby. Rubinius Ruby that is mostly written in Ruby. Apparently this is good for thread-safe running of multi...

Retraction – Installing Ruby 1.9 on a Mac.

Nothing is worse than having to print a retraction, one of my posts  is on the lightest way of installing a new Ruby on the Mac. Turns out the rbenv and ruby-build do indeed require Xcode. Even though the website and even some people believe it's not required , ruby-build needs a compiler. Suddenly the case for rbenv is nil, null and void. I am a bit upset that this requirement is not clear from the documentation, and its harder configuration, coupled with the need to always preface your Rails commands with 'bundle exec' and its lack of advantage of rvm in size, make rbenv a looser and I can't recommend it anymore. I have just switched to RVM , which while it requires the behemoth of Xcode or possibly just the ' command line tools for Xcode ' is a far better choice. RVM or Ruby Version Manager, allows you to create gem sets per project that then can be shared with the project itself. It's command line interface is super polished, and there is way more ...

Why Ruby is the perfect* teaching language.

I was reading Avdi's post on a new programming langauge designed for learning called Grace, like Skitch, LOGO and others before it, it fails. It fails because computer language designers keep looking in the wrong place. In the PDF on their presentation they discard python because it has "inconsistent method syntax" like that is a point of contention in learning a language. Look at normal speaking languages. Are they consistent? Heck no. They're full of exeptions and irregularities. The more regular the easier it can be to learn yes, but irregularity doesn't equal difficulty. Some irregularity is expected. The number one thing a learning language needs to be is readable . It's like the difference between html and markdown . HTML is simple, consistent and easy to understand but it's near unreadable. It's deeply nested, it's interrupted by obtrusive markup and closing tags. While markdown is eminently readable. That makes it easy to learn. So la...

Installing (painlessly) Ruby 1.9.3 into Mac OS X

Update: Nov. 2012 . I no longer recommend rbenv, I now recommend RVM (ruby version manager) since both require Xcode to function. And the latest JRuby (1.7.0) now defaults to 1.9.3 and doesn't require the --1.9  switched to be passed or a JRUBY_OPTS variable set. (I also updated the post to reflect the proper capitalization of JRuby vs. Jruby, which is incorrect.) ----- Installing the latest version of Ruby on Mac OS X is a bit more complicated than it should be. There are no .dmg installers and the built in version 1.8.7 is quite old now.  There are various ways of installing ruby on a Mac and here is a list from the least painful to the most annoying. Using JRuby or MacRuby are the least painful way to install Ruby 1.9 to OS X. They come with their own .dmg installer, it's easy and fast. Unfortunately JRuby (although it includes 1.9) defaults to 1.8 so you have to go into your .bash_profile file and add a line there. See previous blog here . Note that because ruby...

Finally Got RESTful

REST is a web-site architecture paradigm. One that's used in Rails and that for the longest time seemed to involve some mumbo-jumbo. But it's way simpler than I originally thought, but also easy to confuse. So what is REST? REST stands for Representational State Transfer. But basically what this means is that instead of there being a digital asset in a location all there is a representation of that digital asset. Or in other words instead of finding a file in a particular location, all there is is a file-path-location that is interpreted as a command  for a digital asset that lies elsewhere. This has two consequences: the file you are seeking does't have to exist, the Rails server can create it ad hoc, and you don't have to go to one page to view an asset and another page to modify it. The server can interpret different commands along with the path as a way of manipulating that asset. How REST works is kinda besides the point. I already got that, but not why it di...

Getting Growl-like notifications on Ubuntu 11.10 in Rails 3.1

I'm doing Michael Hartl's Rails Tutorial  book this weekend and I wanted to get the notifications right. Took me a while to get it to work. Since this is a fresh install of Ubuntu 11.10 Oneiric Ocelot I'm running into a few issues. I'm running ruby 1.9.2 using rvm since Ubuntu only included 1.9.1. I'm also using the Gnome 3 Shell as I prefer it to Unity. 1) The first issue was that the execjs gem that is installed by default by Rails 3.1 requires the Spider Monkey Javascript engine to be installed in Ubuntu. ( sudo apt-get install libmozjs185 libmozjs185-dev or use Synaptic and search for "spidermonkey" which is what I did ). Or you can comment it out of your Gemfile as it doesn't seem to affect anything in the tutorial. 2) The second issue was that I wanted the nice Growl-like notifications for autotest . The instructions on the automate everything website got me there but don't install the gem 'redgreen' it's buggy. Here is ...

Next Ruby Project -- Rails Project -- Ephemeris

This is going to be my first rails web app, but I'm going to build the heart or the mechanics of it in Ruby first. This is my next big personal project. I'm putting it ahead of all other ones because this one has major usability. What is Ephemeris? Ephemeris is a program that calculates the phases of the moon and the time for sunrise, sunset, moonrise and moonset for any location on the planet. There's a lot of math I don't have a clue how to do involved. So here are the end goals By supplying your location (say Los Angeles) and a date you can get: MoonPhase in a neat little graphic of the day Sunrise, sunset, moonrise and moonset Ability to create a moon calendar for the month(s) supplied. Ability to create a sunrise/set moonrise/set table for month(s) supplied. Give the exact time of full moon and new moon.  The all the times should be accurate to the timezone of your location.  First step is finding the math to calculate this and code it in Ruby. Th...

Setting JRuby to run in 1.9.2 mode by default

JRuby comes with both 1.8.7 and 1.9.2 (currently) and the default is 1.8.7. If you want to change it you have to feed an option at the command line: $jruby --1.9 This weekend I had to migrate a rails program that I hadn't written and the author used 1.9.2 notation so it would not run on JRuby with the default settings. So I had to add the version option with every command which gets old quick. I looked online for a way to make this automatic (and on my JRuby books) but could only found the way to do it on a Mac or Linux but all Windows references just mentioned nebulous concepts such as environmental variables and seemed to be different from which version of Windows you are using. Here is how to do it. Windows 7: Got to Control Panel, System. Wine (Crossover 10): Java 6 version 21 will run on Wine (at least on Crossover 10) and you can install JRuby on top of that. To set environmental variables you have to go to regedit and set it there manually. Follow th...

Configuring Netbeans for JRuby

Netbeans is an IDE (Integrated development environment) originally for Java. However version 6.9 (but not 7) can also be used with Ruby and JRuby. Because it's an IDE for Java, Netbeans is ideal for setting up JRuby on Rails projects. And here is how. First make sure you Download the Java version of Netbeans 6.9.1 or the complete version. It's easier to add Ruby plugins to the Java Netbeans than viceversa. Install the Ruby plugins to Netbeans. Create a new project and follow the screen shots below.  Netbeans comes with it's own bundled version of JRuby (1.5) so go to Manage Ruby versions to set a different version or even a C-Ruby version if you want. The server needs to be set to Webrick to get the normal Ruby on Rails server. If you are setting up a JRuby on Rails project make sure you mark the: " Access Database using JDBC " And like that you have a properly configured Rails file for JRuby, without having to use the "--template...

Installing JRuby for Rails

JRuby is Ruby that runs on the Java Virtual Machine (JVM). It has the advantage of being cross platform (Linux, Mac and Windows) and being able to run on a 64-bit machine (important if you have a Windows 64-bit machine since the normal Ruby is 32-bit only for now on Windows). It at present installs both Ruby 1.8.7 and 1.9.2 but defaults to 1.8.7, but in the future the default will change to 1.9. The Windows installer meshes really well with the operating system and aside for the first time start-up time (it loads Java first) it's equally fast. However to run Rails on JRuby some things need to be taken care of. This set up should have you in good shape for Michael Hartl's book: Ruby on Rails 3 Tutorial. 1. Preliminary things you need. JRuby on Rails is a mostly platform agnostic installation. It can be installed on any OS that runs the JVM or Java as it is commonly known. You probably already have it on your machine. But in case you don’t, you need it to install JRuby. Verify ...

Programming Tool: Komodo Edit 6

As I've written before I'm learning Ruby, a programming language. I use two computers one Mac (the laptop) and one PC (the desktop) and I'd been looking for a good text editor to do my programs. If you've never programmed before, a program is merely a text file that has been written in a language the computer can translate to machine language. The language the program is written on is called a programming language and there are many with different strengths and weaknesses among them: C, C++, C#, Ruby, Python and Java. To write programs two tools are typically used a text editor and an IDE or integrated development environment. For Ruby an IDE is bit of an over shot, so I was looking for a text editor. The best text editor from everybody I've talked to is Textmate (or BBedit though they're both very similar). Only problem is that it's $60 and only available for Mac (there is a port of Textmate to Windows called E-Text and that one is $40). So I looked fo...

Tricky Shoes (Ruby pt. 14)

Finally found an explanation for the weird behavior of variables in Shoes. There is a fork of Shoes called Green Shoes that combines Ruby with Ruby- GTK to redo-shoes. Actually a neat idea since it extends Shoes to a larger windowing system. In the manual for the project I found something interesting about Shoes. In Shoes each window is the unit. The stack blocks are weird. They look like normal blocks but are sub-set block of the window unit. The command para (for paragraph) which displays the text on a block is really self.para and self is from the Shoe's window. Additionally all instance variables inside and outside stack blocks, are held inside the window unit too. This is key and not well explained in the documentation on Red Shoes. Still haven't figured out how to make my temperature conversion GUI program and I'm seriously tempted to just learn Xcode and code it on the Mac with Macruby , but Shoes with all it's faults is cross platform and that has huge ...

GUI Maze of Death (Ruby Pt. 13)

One of the challenges of programming is interacting with the user. The terminal presents few options. I'm working on a temperature conversion program. Which so should use radio buttons. You enter the degrees enter the number, click on the degrees you want to convert to and the answer pops out. Cool so I thought this would be the perfect opportunity to learn Shoes. A lightweight GUI for Ruby. But no. Gui programming is like trying to find your way through a maze. Except blindfold, dizzy and no minotaur motivation. This is not really a commentary on Shoes, which is actually quite easy, but on the tutorials and resources to learn it. In that there are so few and they tend to assume a high level of programming mastery. Designing the Gui is not exactly easy but the real maze is trying to link it to a the heart of the program. I've been trying to write a program that changes the temperature from Fahrenheit to Celcius thinking this was a great place to use radio buttons or list ...

Sprint Fast, Squished Frogs and Overloading. (Ruby Program pt. 12?)

I've decided to take an online Ruby course. I started last Saturday. I've put aside the FBI-website reading project to the side a bit, since I need to get more core basics before I tackle that one. One of the first exercises on the class has to do with the " sprintf " documentation and this bit of code: puts "%05d" % 123 This looks like the modulo operator from before (see my previous blog ) so it left me totally confused . I tried to get more explanations but all seemed to follow the equational approach: see the formula, see how it works, understand. This approach  must work for some people but I totally don't "get" it that way. That step three, just doesn't happen. I look at the equation nod stupidly and fail to grasp it. When I look at spritf , I think Sprint Fast and imagine runners gunning it and don't get what the percentage sign is doing there. Finally I happened upon Why's explanation in his book, which soooo needs a...

Delving into the 7th level of Unicode Hell (Ruby program)

When the three brothers defeated the Titans and had the whole cosmos to divide among themselves, it was the oldest ᾍδης who got the Underworld. A kingdom I seem to be quickly descending on my innocent trip to down Ruby programming. I set out to do a little program that would reverse text like ᾍδης particularly useful for Hebrew and Arabic. But then I found myself entering... A Glance into the Unicode Underworld Kairon ferried me across while I was sleeping, because suddenly my little program is facing a huge issue. Ruby doesn't fully support Unicode.  As I searched deeper into this Unicode world I found I was in bigger trouble than I originally thought. ANSI, the common encoding of Ruby, is 8-bit encoding. So I thought, well Unicode must be 16-bit encoding, more bits more letters -- problem solved! Just find a way to split off two 8-bit character chunks at a time and stack them on a new line and voilà! We have the text reversed. I even found a way of doing that. If you have a s...

New Project (Ruby Programming pt. 11)

Ok so with the reverse-text project, all I need to do is learn how package it as an app or exe. Not so simple but since it's not covered in my books I'll have to ask it out. So I've come up with another project. It's a Rails project so it's way in the future, but I'll guide myself in that direction. I want to make a website that calculates how much of your taxes go to different programs each year and presents it in a nice-looking pie graph . It would give you the average federal tax paid in your state and how many dollars of each went to whatever project. The cool thing is that this website would be a data aggregating website . It would scour the websites that have the data and compiles the graph for you. I'll host it on Heroku. Of course, I'm way far away from diving into Rails but, BUT , Ruby can parse websites. So I'll make a stand-alone program that does that first, and renders its output in HTML all ready to be dropped in on a rails project. ...