4/20/2008

Return Value

What is the value returned from function in ruby?

It is not important which is method or local function.

The ruby's manual says that the value returned is result of last computed in function.

In fact, If you want to write a function to carry back a handle, you have to type next code.


def function()
  handle = Yourclass.new()
  handle
end

4/16/2008

Lower Bound Search

Template function lower_bound is work binary search.
STL provides this function.

The forth argument is function pointer to compare two element in STL's specifications.

If When I use this function with basic type and my class has compare operator, I cut the forth argument.

But, Visual Studio 2003 points the way is error.

I wonder it.

4/15/2008

Moment of Inertia

Yesterday, I brushed up the Moment of Inertia also called Angular Mass.

In my textbook, Clamped Bar's moment of inertia is 1/3 times mass of bar times square length of bar.

I first can not understand it, Especially 1/3.


But, Moment of inertia's formula is integral square distance times point mass.

Therefore, In definite integral 1/3 appeares.

4/10/2008

Binary Search with STL

I have used large data with c++.
Something to do while I use large data is to find target data.

Large data is arranged with order of rise.

Therefore I appropriated Binary Search to algorithm finding target data.

This way is efficient, Processing time became small.

Next codes are Binary Search with stl in c++.

#include < algorithm >
#include < iostream >
#include < list >
using namespace std;

class Data
{
public:
  int m_true_id;
  int m_inner_id;

  Data(int ti = -1, int ii = -1);

  void output(void);
  bool operator < (const Data& arg){
    if( this->m_true_id > arg.m_true_id ) return true;
    return false;
  }
};


Data::Data(int ti, int ii) :
m_true_id(ti),
m_inner_id(ii)
{
}

void Data::output(void)
{
  cout << "Data[" << m_true_id << "]" << endl;
  cout << endl;
}


int main(void)
{
  list< Data > nodes;
  for(int i =0; i < 10; i++){
   nodes.push_back(Data(i));
  }

  for( list< Data >::iterator ite = nodes.begin(); ite != nodes.end(); ite++){
    (*ite).output();
  }


  list< Data >::iterator target
= lower_bound(nodes.begin(), nodes.end(), a);

  target->output();


  return 0;
}


Keypoint is define operator < in class Data.

4/08/2008

Ruby's Regular Express

3 or 4 days ago, I try one program with ruby.

It's contents that transform words matched keyword in regular expression into other words.

Keywords are ProjectName , author and date.

Next codes are this program.


#!/usr/bin/ruby
class TextValue

    # class's member
    TABLE = {
    "ProjectName"=>"AAA",
    "author"=>"bulldog",
    "date"=>Time.now.strftime("%Y-%m-%d")}

end

begin

    TextValue::TABLE["ProjectName"] = ARGV[1]
    # open the file
    tmp = open(ARGV[1])
    while line = tmp.gets()

      if /\$(\w*)\$/ =~ line then
        print line.gsub(/\$(\w*)\$/, TextValue::TABLE[$1])

      else
        print line

      end
      end

    tmp.close

end

4/02/2008

Desperate Housewives

I like Desperate Housewives , and My girl friend too.

This drama makes me smile.

In Japan, Rental video shop starts arrange season 3.

We enjoy to see it.