Monday, January 9, 2012

Proc vs lamda in ruby

The are two major difference between procs and lambda 

1.) A proc can break the parent method, return values from it, and raise exceptions, while such statements in lambda will only affect the lambda object. 

def foo
  f = { return "return from foo from inside proc" } # control leaves foo here
  return "return from foo"

def bar
  f = lambda { return "return from lambda" } # control does not leave bar here
  return "return from bar"

puts foo # prints "return from foo from inside proc"
puts bar # prints "return from bar"

2.) The other difference is unlike Procs, lambdas check the number of arguments passed.

def some_method(code)
  one, two = 1, 2, two)

some_method({|first, second, third| puts "First argument: #{first} and Second argument #{second} and Third argument #{c. third}"})

some_method(lambda{|first, second, third| puts "First argument: #{first} and Second argument #{second} and Third argument #{c. third}"})

# => First argument: 1 and  and Second argument 2 and Third argument NilClass
# *.rb:8: ArgumentError: wrong number of arguments (2 for 3) (ArgumentError)

We see with the Proc example, extra variables are set to nil. However with lambdas, Ruby throws an error instead.

Sunday, January 8, 2012

rhomobile tutorial and Example

If you are new to Rhomobile and don't know how to start with Rhomobile products. Then may be this blog will helpful to you. I have consolidate all the tutorials and Examples.






I will keep updating this with new example and sources to learn Rhomobile products

Rhomobile Ajax Calls using JQuery Mobile

Note: This will only work on IPhone,Android and Blackberry(version 6 and above) 

There is common scenario to use Ajax with Rhodes but I was not able to find good source to use them so thought of blogging it. I will be using Jquery Mobile framework which is by default with Rhodes.

In this example I will load some text inside a div on clicking of a button.

Step 1.) Create a div and button on a page
<input type= "button" id="some-button"/ name="button" value="Load Value By Ajax">
      <div id= "some-div">

Step 2.) Create a Javascript 

Step 3.) Create a action ajax_method in controller
def ajax_method
    @some_value = "Its all about ruby ---" +@params['some_variable']
    render :action => :ajax_method, :layout => false, :use_layout_on_ajax => false

Step 4.) Create a ajax_method.erb file
<i><b><%= @some_value %></b></i>

It is just a very basic example you can do many more things like error handling, adding loading image etc.