Category Archives: Uncategorized

tsobi is comes from a Ghanaian language called ‘Ga’. It means doll or more so ’stick child’ like pinnochio

look at an implementation in Ruby to produce ‘tsobi’ :-) .

Author : Selasie Agbavor

Date : 25/10/08

source : tsobi.rb

# souce code

def tsobi_1
puts ‘  0   ‘
puts ‘  .   ‘
puts ‘/ . \ ‘
puts ‘ / \  ‘
end

def tsobi_2
puts ‘  0  ‘
puts ‘\ . /’
puts ‘  .  ‘
puts ‘ / \ ‘
end

def jump(time)
loop do
tsobi_1
sleep time
system ‘cls’
sleep time
tsobi_2
sleep time
system ‘cls’
sleep time
system ‘cls’
end
end

jump 0.5

I was wondering why the integer class in Ruby lacked a method like octal, so i guess it would be nice to throw one in myself to make things easier.

class Integer

    def octal
        require ‘binary’
        num = self
        num_to_binary = num.binary
        array = %w(0 00)

        if num.size % 3 != 0
            array.each do |obj|
                concat_str = obj + num_to_binary
                if concat_str.size % 3 == 0
                    num_to_binary = concat_str
                end
            end
        end

        octal_mappings = {
             ‘000′ => ‘0′,
             ‘001′ => ‘1′,
             ‘010′ => ‘2′,
             ‘011′ => ‘3′,
             ‘100′ => ‘4′,
             ‘101′ => ‘5′,
             ‘110′ => ‘6′,
             ‘111′ => ‘7′
        }

        split_str = num_to_binary.scan(/…/)
        hex_array = []

        split_str.each do |str|

            hex_array.push octal_mappings[str]

        end

        hex_array.to_s
    end
end

Hey, do you want to view all the possible arrangement of the letters of a word, then try this.

 

#   file   : wordperm.rb

#   Author : Selasie Agbavor <volsbit@gmail.com>

#   Date   : May, 16 2008

#   Time   : 14:20:00 G.M.T

 
class String

   def shuffle

       self.split(//).sort_by{rand}.to_s

   end

end
class Integer

    def fact

        if self.zero?
            1
        else
            self * (self-1).fact
        end
    end

end
word = gets.chomp

array_of_letters = word.split(//)

hash = Hash.new(0)
array_of_letters.each{ |lett|

    hash[lett] += 1

}

counts_greater_than_one = []

hash.each_value{ |val|

    counts_greater_than_one << val if val > 1

}

if !counts_greater_than_one.empty?

    product = 1
    counts_greater_than_one.each{ |num|

        product *= num.fact

    }

    permutation = word.size.fact / product
   
else

    permutation = word.size.fact
   
end

 

already_shuffled_words = []
count = 0

while count < permutation

    shuffled_word = word.shuffle

    if !already_shuffled_words.include? shuffled_word

        puts shuffled_word
        #sleep 1  # optional
        already_shuffled_words << shuffled_word
        count += 1

    end

end

puts “\n”

puts “#{word} can be arranged #{permutation} ways taking all characters”

looking for a library to do some basic statistics computations, check this out, then.

#  Author : Selasie Agbavor <volsbit@gmail.com>

#  Source : Ruby 

 

class Array
  def even?
    self.length % 2 == 0
  end

  def sum
    sum = 0
    self.each { |i| sum += i }
    sum
  end
end
class Stats
  def initialize(*data)
    @data = data.map { |s| s.to_i}
    @size = data.length
    variance
  end

  def size
    @size
  end

  def data
    @data
  end

  def mean
    mean = @data.sum / @size.to_f
  end

  def mode
    frequencies = Hash.new(0)
    @data.each{ |i| frequencies[i] += 1 }
    highest_frequency = frequencies.values.max
    frequencies.keys.select { |key| frequencies[key] == highest_frequency }
  end

  alias :average :mean

  def median
    max_index = @size – 1
    sort_data = @data.sort
    if @data.even?
      mid_1 = max_index / 2
      mid_2 = mid_1 + 1
      (sort_data[mid_1] + sort_data[mid_2]) / 2
    else
      sort_data[(max_index + 1) / 2]
    end
  end

  def variance
    devi_set = @data.map { |datum| datum – mean }
    sum = devi_set.map{ |datum| datum * datum}.sum
    @variance = sum / @size #  or @size – 1
  end

  def standard_deviation
    Math.sqrt(@variance)
  end

  def range
    @data.max – @data.min
  end

end

#  create a data object like so :

#  my_school_data = Stats.new(2, 3, 4, 5, 6,  7)

#  puts my_school_data.mean => 4.5

#  puts my_school_data.standard_deviation => 1.70782512765993

Ruby is a dynamic, reflective, general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was initially developed and designed by Yukihiro “Matz” Matsumoto.

Ruby supports multiple programming paradigms, including functional, object oriented, imperative and reflection. It also has a dynamic type system and automatic memory management; it is therefore similar in varying respects to Python, Perl, Lisp, Dylan, and CLU.

In its current, official implementation, written in C, Ruby is a single-pass interpreted language. There is currently no specification of the Ruby language, so the original implementation is considered to be the de facto reference. As of 2008, there are a number of complete or upcoming alternative implementations of the Ruby language, including YARV, JRuby, Rubinius, IronRuby, and MacRuby, each of which takes a different approach, with JRuby and IronRuby providing just-in-time compilation functionality.

My passion for automating processes led me to discover programming. I started using Qbasic way back in my first year at Achimota School. I usually had to skip snack breaks just to try out my little math programs which I usually worked on in between class hours. After living school I decided to, try out something new that’s when I met Lorenzo at Ghana-Indian Kofi Annan Excellence in I.C.T. He taught us how to program the Ruby way.Since then I’ve not stopped programming the Ruby way.

Accra.rb is a Ruby user group located here in Accra,Ghana. Our main focuss is to deliberate on Ruby issues and programming in general. We meet bi-weekly on Saturdays. You can find the Accra.rb in google groups.All are invited to learn this powerful scripting language.