Tag Archives: ruby

Private Member Variables in Ruby

How can you create private member variables in Ruby? If you’re used to the attr_accessor helper, that won’t work–that makes your member variables public. You can use the @ notation, eg. @variable_name. (Attr_accessor just gives you free getters and setters.) Continue reading

Posted in Development | Tagged , , , | 1 Comment

Block Comments in Ruby

How do you comment out a chunk of code? Other programming languages (C++, Java, etc.) have a slash-star style of block comments, like so: /* */ … but what about Ruby? Does Ruby have a mechanism for this, too? The answer is YES; BUT, there’s a catch! If you don’t watch whitespace … Continue reading

Posted in Development | Tagged , , | 2 Comments

Conditional Code

You’re writing some code for a new client. Debug statements litter the code. Oops, you sent that to the client. Oops, you’re printing out credit-card numbers. Identity theft ensues. Don’t move to Mexico–you can actually handle this problem in an elegant way by using conditional code statements INSIDE your class … Continue reading

Posted in Development | Tagged , , | Comments Off

String Replacement in Ruby

How can you replace strings in Ruby? There are a couple of options; there’s the tr function, which takes two sets of characters, and substitutes them; and there’s the gsub method, which makes a global replacement based on a regular expression. Powerful stuff! Continue reading

Posted in Development | Tagged , , | Comments Off

Ruby Command-Line Arguments

How can you access command-line arguments in Ruby? Through the special array called ARGV. It’s a zero-indexed array of your command-line arguments; so ARGV[0] is the first, ARGV[1] is the second, and so on. You can use ARGV.length to get the number of arguments, too. Continue reading

Posted in Development | Tagged , , | 1 Comment

Chaining Array Appends With <<

In Ruby, you can push an element to an array using the << method. This method returns the array, so you can chain appends using it, like so: a << "One" << "Two". Nice! Continue reading

Posted in Development | Tagged , , | Comments Off

Exception and Error Handling

A lot of programming languages have an error-catching (or exception-handling) mechanism, where you can trap errors. How do we do this in Ruby? There are two keywords: begin, and rescue; and the ancestor of all errors is called StandardError. Continue reading

Posted in Development | Tagged , , | 1 Comment

Ruby Interactive Prompt

The great “try ruby!” online interactive prompt is broken! How can we write some code that runs almost the same, albeit in a local Ruby environment? A few minutes of experimentation revealed this little snippet, which works fairly well. Continue reading

Posted in Development | Tagged , , | Comments Off

Functions as First-Class Objects

A lot of programming languages have functions as first-class objects (you can pass them around, execute them, etc.) Ruby has some sort of functionality like that (proc and functors and yield, oh my!), but there’s a simpler solution: the (albeit abusable) eval method. Continue reading

Posted in Development | Tagged , , | 4 Comments

Page Rendering Time

A lot of websites show the page rendering time at the bottom. How can we implement this in our Rails website? We discuss a simple approach, where you wrap between two calls to Time.now (to get the current time). Subtracting them yields the time difference, in fractional seconds. Continue reading

Posted in Development | Tagged , , | 1 Comment