Skip to main content

Posts

Showing posts from 2012

Passing Functions and Lambdas into Functions with Ruby

Ruby's New Style of Lambda Functions f = ->( m ) { p m } f.call( 1 ) #=> 1 Which of course means the same thing as: f = lambda { |n| p n } f.call(1) #=> 1 Ruby Proc Objects p = Proc.new { |n| p n + 2 } p.call(2) #=> 4 Using a Function as a Closure in Ruby def domo( k ) ->(m) { p m + k } end z = domo( 5 ) z.call( 5 ) #=> 10 Function :domo takes a single parameter. Within :domo , we create a lambda that takes a single parameter, and adds that parameter to the value :domo takes in as its parameter. Then, we assign z to be the result of the lambda in :domo with its 'k' parameter loaded with 5. When z is called, we pass (another) 5 to it. This parameter loads the lambdas n parameter. The lambda executes, essentially adding n (5) + k (5) and yielding the result of 10. The thing about closures such as this is, we can load the initial value of the lambda to be whatever we want it to be when assigning the function :domo '

Creating Incrementing Variables in Code with Ruby

I had a project I was working on that required me to assign values to incremental variables from an array.  A quick solution to the problem, using very little meta-coding is below in its most basic form.  One class was my data or value class.  It held the incremental variables I wanted to assign to.  Here It's called, "Foo".  The class that is using this data is "Bar".  Just for spice, I randomized the array values to assign to the data variables. class Foo attr_accessor :i0, :i1, :i2, :i3 initialize @i0 = 0; @i1 = 0; @i2 = 0; @i3 = 0 end end class Bar attr_accessor :f def initialize @f = Foo.new @image_sort = %w{ 1 2 3 4 }.shuffle end def setup @image_sort.each_with_index do | i, idx | @f.instance_variable_set( :"@i#{idx}", i ) end puts @f.i0 puts @f.i1 puts @f.i2 puts @f.i3 end end g = Bar.new g.setup