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

Wednesday, February 20, 2013

7 Languages in 7 weeks: Ruby day 3


Ruby Day 3: Serious change 

The final day on Ruby is all about metaprogramming. It takes you through:
  • Adding methods to open classes
  • Using method missing to produce very human readable syntax
  • Adding functionality to classes via Modules
Having used Java reflection, Java IDL Dynamic Invocation Interface (DII) and the Java IDL Dynamic Seketon Interface extensively when I used to develop IBM's Websphere Message Broker I know how cumbersome any form of metaprogramming is in Java. So I enjoyed playing around with a language that does it much better!

Now to the Self study. Only one this time! Modify the Csv application to support an each method to return a CsvRow object. E.g for the following csv file:

one, two
lions, tigers

allow an API that works like this:

csv = RubyCsv.new
csv.each{|row| puts row.one}

This should print "lions".

Well I decided to work with the following input:

one, two, three
1, 2, 3 
uno, dos, tres

And here is my solution using modules and method missing:

module ActsAsCsv
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def acts_as_csv
      include InstanceMethods
    end
  end

  module InstanceMethods
    def read
      @csv_contents = []
      filename = self.class.to_s.downcase + '.txt'
      file = File.new(filename)
      @headers = file.gets.chomp.split(', ')

      file.each  do |row|
        @csv_contents << row.chomp.split(', ')
      end
    end

    def each(&block)
      @csv_contents.each_index do |index|
        hash = Hash[@headers.zip @csv_contents[index]]
        block.call(CsvRow.new(hash))  
      end
    end
  end

  attr_accessor :headers, :csv_contents
  def initialize
    read
  end
end

class RubyCsv
  include ActsAsCsv
  acts_as_csv
end

class CsvRow
  def initialize(arg)
    @arg = arg
  end
  
  def method_missing name, *args
    key = name.to_s
    return @arg[key]
  end
end


m = RubyCsv.new
m.each do |row| 
  puts row.send(ARGV[0])
end


And that is it for Ruby!

Monday, February 18, 2013

7 Languages in 7 weeks: Ruby day 2


Day 2: Floating Down from the sky


On day 2 we're introduced to code blocks, arrays, hashes and mixins. The more I see code blocks in other languages the more I am looking forward to Java 8. Coming from Java I get particulary jealous of languages with syntaxctic sugar for arrays and hashes; initialising them in Java is cumbersome.

Self-Study

1) Print the contents of an array 4 elements at a time:

Sticking to the verbose solution for now:


array = Array(1..16)

array.each do |x|
  if (x % 4 == 0) 
    print "#{x}\n"
  else
    print "#{x},"
  end
end

Now with slice:

irb(main):014:0> array = Array(1..16)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
irb(main):015:0> array.each_slice(4) {  |slice| puts slice.join(",") }
1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16

2) Tree with a nice constructor:

After finishing this I found a nicer solution with collect, but here is my original:

class Tree
  attr_accessor :children, :node_name

  def initialize( fullTree={})
    @children = []
    @node_name = fullTree.keys.first
    fullTree[@node_name].each do |key,value| 
      children.push(Tree.new( {key=>value} ))
    end
  end
  
  def visit_all(&block)
    visit &block
    children.each{ |c| c.visit_all &block }
  end

  def visit(&block)
    block.call self
  end
end

ruby_tree = Tree.new({ 'grandad' => { 'dad' => { 'child 1' => {}   }, 'uncle'   => {}  } })

ruby_tree.visit_all { |t| puts t.node_name  }

3) Ruby grep!

pattern = Regexp.new(ARGV[0])
file = File.new(ARGV[1], "r")

counter = 0
file.each_line do |line|
  if line.match(pattern)
    puts "#{counter}: #{line}"
  end
  counter = counter + 1
end

Day 3 to come some time this week hopefully!

Saturday, February 16, 2013

7 Languages in 7 weeks: Ruby day 1

Having decided I spend far too much time programming with Java I added the 7 languages in 7 weeks to my Amazon wish list and come Christmas it turned up.

Day 1: Finding a Nanny (Ruby)

Firstly we're introduced to Ruby's interactive console. My first conclusion is that all languages should have an interactive console!

A few differences to Java that stand out:

  • Interpreted: Nuff said.
  • You have the unless conditional so you can write lines that read quite nicely: order.calculate_tax unless order.nil?
  • Everything is an Object, including primitives.
  • Duck typing: You can call any method on an object and if it has it at runtime it will be called. You don't have to have classes inherit from the same class to be used in the same way.

The homework

Print the string Hello, World.

puts 'hello, world'

For the string "Hello, Ruby" find the index of the word "Ruby"

irb(main):007:0> "Hello, Ruby!".index('Ruby')
=> 7

Print your name ten times

irb(main):009:0> for i in 1..10
irb(main):010:1> puts "Christopher"
irb(main):011:1> end

Print the string "This is sentence number 1" where the number 1 changes from 1 to 10

irb(main):009:0> for i in 1..10
irb(main):010:1> puts "This is sentence number #{i}"
irb(main):011:1> end

Run a Ruby program from a file

chbatey ~/dev/7languages/ruby $ cat day1.rb 
#!/usr/bin/env ruby

puts "Hello"

Bonus problem: Guess a number between one and ten:


#!/usr/bin/env ruby
rand = rand(10)
puts "A number between  0 and 9"
user = gets().to_i

if user < rand
  puts "Less"
elsif user > rand
  puts "More"
else
  puts "Equal equal!!"
end

puts "The number was #{rand}"

Well that's day one of Ruby done. I look forward to tomorrow!