I have a bunch of links I need to build on page, links that are a modification to the current path. I wanted to be able to do my modifications and return the modified path (@updated_path) without changing the original path variable (@path).
This isn’t difficult stuff, the only thing that I had to ponder [...]
Archive for the ‘Ruby’ Category
Using ensure to reset a value
Posted in Ruby on April 25, 2009 | 3 Comments »
Ruby: inject
Posted in Ruby on November 14, 2008 | Comments Off
Inject is a powerfully cool method provided by the Enumerable module. At first I was a little confused by the name. I didn’t know what it meant. Here’s a simple example:
an_array = [1, 2, 3, 4, 5, 6]
def x10(v)
v * 10
end
an_array.inject([]) do |sum, num|
sum << x10(num)
end
=> [10, 20, [...]
Matz Google Tech Talk
Posted in Ruby on February 25, 2008 | Comments Off
Here are some notes from the talk Matz gave. It doesn’t every little detail of the whole ~31 minute talk with a ~19 minute question/answer session. Just some of the points I wanted to remember.
1.9 Incompatibilities with 1.8
block parameters local variables only in pipe blocked scoped #The following will not work in 1.9 # [...]
collect and flatten
Posted in Quick Tips, Ruby on February 12, 2008 | Comments Off
I run into this scenario many times and never really thought about changing it until recently. It’s very common, I have an array of values and I want to perform an action on each item. My return value would be an array of the results from the action.
Here’s a sample lookup that most [...]
exists? == not_nil?
Posted in Ruby on December 12, 2007 | Comments Off
Apparently Facets adds the exists? functionality in the form of not_nil?. It seems there’s a lot of goodness in Facets that I will now have to investigate. Thanks to Trans for giving me the heads up.
Here’s to not re-inventing the wheel. Cheers.
I like readable code is it’s one of the nice attributes of Ruby. Today I had some code that could use some improvement.
Ruby has a method called nil? that you can send to any object. When I want to test is that object is not nil? I could:
#use unless unless [...]
Mikey
Posted in RSpec, Ruby, RubyOnRails on December 8, 2007 | Comments Off
So, I have a site that synchronizes data with another server. It does this via after_create, after_update and after_delete triggers on some models. All of those triggers use the same method that returns a connection object which has various methods such as:
# SyncServer::connection method module SyncServer def [...]
nearest power of 2
Posted in Ruby on November 29, 2007 | Comments Off
This is one of the reasons I’m a fan of Ruby:
class << Math def log2(n) log(n) / log(2) end end
class Integer def nearest_power_of_2 2**(Math.log2(self).floor) end end
47.nearest_power_of_2 => [...]
gem server
Posted in Quick Tips, Ruby, RubyOnRails on November 20, 2007 | Comments Off
In the category of why I haven’t I used this yet (duh!):
run: gem_serverto run as daemon: gem_server –daemon
then access: localhost:8808
Documentation for your installed gems. Nice.
If you don’t have any documentation, run: gem rdoc –all
This will generate the docs for all the gems installed on your system.
Note: gem_server has been deprecated in favor of [...]
Ruby Interview Questions – 101
Posted in Interview Questions, Ruby on November 20, 2007 | 1 Comment »
Interview questions/answers for an entry level Ruby developer.(Use these questions in conjunction with the 101 level Rails questions)
Q) How do you comment out a block of code?
A) Use =begin and =end.
=begin def my_commented_out_method end =end
You could use successive # signs, but that’s just tedious:## def my commented_out_method# end#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) How do [...]
