Friday, March 28, 2014

Changes 3/28/14

Ruby:

  • Wrote a program to explore block functionality in ruby
  • It is a bit more complicated than I anticipated and I might split the second nested block into its own variable
  • class BlockStorage
      def initialize(&block)
        @block = block
      end
    
      def execBlock(string)
        if @block.respond_to? "call"
          @block.call(string)
        end
      end
    end
    
    bs = BlockStorage.new() do |str|
      arr = str.split(" ").reverse
      arr2 = arr.map do |e|
        puts "#{e}, #{arr.first}, #{arr.last}"
        if e == arr.first
          e.gsub(/\W$/, '').capitalize
        elsif e == arr.last
          e.downcase
        else
          e
        end
      end
      arr2.join(" ")
    end
    
    puts bs.execBlock("Block coding is pretty darn cool.")
    # Outputs  'Cool darn pretty is coding block'
    puts bs.execBlock("It really streamlines the process.") 
    # Outputs 'Process the streamlines really it'
    
    

1 comment:

  1. Better you than me, Mr. Hirschberg, better you than me...

    ReplyDelete