Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Monday, January 30, 2012

Learning about closures in ruby

Per Wikipedia, a closure is defined as: "In computer science, a closure (also lexical closurefunction closurefunction value or functional value) is afunction together with a referencing environment for the non-local variables of that function.[1] A closure allows a function to access variables outside its typical scope."

Taken on just its pure definition, it's a little difficult to conceptualize, especially for someone like me who comes from a mostly iterative programming background. So let's look at some sample code (I modified some sample code about Ruby from Wikipedia)



puts "learning about closures"
def create_closure(initial_value = 0)
  closure_value = initial_value
  return Proc.new do |x|
    closure_value = x
  end, Proc.new do
    closure_value += 1
  end, Proc.new do
    closure_value
  end
end
one, two, three = create_closure
myval = 21
puts "one.call(myval) == #{one.call(myval)}"
puts "two.call == #{two.call}"
puts "two.call == #{two.call}"
puts "three.call == #{three.call}"


This outputs:

learning about closures
one.call(myval) == 21
two.call == 22
two.call == 23
three.call == 23




Each proc here is a closure, because the scope of each proc is bound to the scope of where it was defined. In this case, all of the procs are bound to the scope of the create_closure function.

So you can see that even though the execution of create_closure is finished, each proc still has access to the same local closure_value variable that was defined within create_closure function.


Tuesday, April 12, 2011

$.parseJSON with Rails 3's .to_json

This one stumped me for a bit today. If you had any old code from Rails 2.3.* and converted it to Rails 3.0.*, watch out for any instance variables in your controller that you call to_json on for processing by jQuery's $.parseJSON in your view.

Because the default in Rails 3 is to make all strings html safe, you no longer need to use the "h" helper method in your views. However, this means that code like this will no longer work:

controller: @temp = {:hello =>; "world"}
view: alert($.parseJSON("<%= @temp.to_json %>"))

You will get invalid JSON errors, because the "safe" string is not in a valid format for parseJSON. To solve this, use the Rails 3 html_safe helper to tell the view that this string is ok to send through as is:

controller: @temp = {:hello =>; "world"}
view: alert($.parseJSON("<%= @temp.to_json.html_safe %>"))

Sunday, October 24, 2010

i18n bug in Rails




I just ran into a bug when we installed a new gem in the same Ruby install that runs both our test environment and our Redmine install. The problem is that the new i18n gem is not compatible with rails 2.3.5.

The solution was to change this line
gem 'i18n', '>= 0.1.3'

to this
gem '18n', '<=0.4.1'

To ensure that Redmine grabs an earlier version of the i18n gem. Hopefully this saves someone else out there from a few hours of frantic googling.