Sonntag, 21. November 2010

my first ruby bug?

as announced in my last post I fixed regexp_test.rb, hash_test.rb and open3_test.rb. If you are interested in the details have a look at github: https://github.com/SlevinMcGuigan/SMG--learn

But this seems to be my first ruby bug which I sumbled across while fising hash_test.rb. 1.8.7 behaves different compared to 1.9.2
testhash = {
      '1' => 'a',
      '2' => 'b',
      '3' => 'c'
    }
puts "#{testhash.index('a')}"         # => 1
testhash['3'] = 'a'  
puts "#{testhash.index('a')}"         # => 1
testhash.delete('1') 
puts "#{testhash.index('a')}"         # => 3
testhash['4'] = 'a'
puts "#{testhash.index('a')}"         # => 3
testhash['3'] = 'x'
puts "#{testhash.index('a')}"         # => 4
testhash['3'] = 'a'
puts "#{testhash.index('a')}"         # => 3
testhash['1'] = 'a'
puts "#{testhash.index('a')}"         # => 3 with ruby 1.9.1p243
                                      # => 1 with ruby 1.8.7

  it "returns the corresponding key for value" do
    new_hash(2 => 'a', 1 => 'b').send(@method, 'b').should == 1
  end


But shouln'd there be somthing like
  
  it "returns the first corresponding key for value if there is more then once occurance of value" do
    new_hash(1 => 'a', 2 => 'b', 3 => 'a').send(@method, 'a').should == 1
  end

As I didn't want to raise a newbe-nonsense-defect in the issue tracking system http://redmine.ruby-lang.org/ and as I didn't wan't to ask silly-newbie-questions in the mailing list http://www.ruby-lang.org/de/community/mailing-lists/#ruby-core I went for a german forum (which is directly linked from www.ruby-lang.org): 

This is my first post: bug in ruby 1.8.7 in hash.index?

Samstag, 6. November 2010

Cygwin ain't Linux

Today I looked at my failing tests in open3_test.rb. One of them failed here:
stdin, stdout, stderr = popen3('cd this_folder_does_not_exist')
After some investigation I found out that popen3 in Ubuntu expects a "real" executeable, shell build ins like 'cd' cause "Error:ENOENT". This is one more difference between cygwin and Ubuntu. Wanna try?

Ubuntu
egon@egon:/home/egon> ruby -e '%x[cd]'
-e:1:in ``': No such file or directory - cd (Errno::ENOENT)
        from -e:1:in `
'


cygwin
$ ruby -e '%x[cd]'
$

As my test was just a learning test I looked for better ways to interact with the underlying OS. These sites helped me a lot
http://stackoverflow.com/questions/2232/calling-bash-commands-from-ruby
http://tech.natemurray.com/2007/03/ruby-shell-commands.html
Result: I changed my tests and now I use the classes Dir and File interact with the underlying OS

As a side result I noticed somthing weird with Test::Unit. I don't know whether this is a bug or a feature. I will find out later on, hence I added a ToDo into my WiKi. See http://slevinmcguigan.wikispaces.com/ToDo's

Donnerstag, 4. November 2010

encode, decode, transcode, dog Kot (ha ha... not funny, even if you are german...)

Yesterday I stumbled across a failing test because of this
assert('Susi Sorglos lebt ein schönes Leben' =~ /lebt/,'Ja, Susi lebt :)')
This passed before I went from cygwin to Ubuntu. Why?


Once again the "bible" helped me. It's all about encoding! Ruby 1.9 introduced much more consequent encoding/transcoding/... and when going from cygwin to ruby I went from 1.8 to 1.9. Wanna re-read the encoding/transcoding/... stuff? Start with p92 and then goto chapter 17, p251

Bottom line for me. As long as I consider myself ruby noob I will only use ASCII.

I still have 130 pages to go until I reach chapter 17 (hmmmm... What about inventing ruby skill levels? Simiar to computer games? In this case I would still be a level 1 programmer. Assume you really understood all the contents of the "bible", would you then be a level 26 programmer...? And what follows next? What to do to become a real Yoda-like ruby master? :D )

Mittwoch, 3. November 2010

long time no see... Glad to see you Egon

To get a long story short...
- I noticed that there is not ruby 1.9 for cygwin (yet)
- I noticed this when the example given in the "bible", p21 didn't work
- I tried to use the ruby 1.9 windows installer verson in cygwin (I want to have a powerfull and usefull shell, not the windows cmd.exe crap), some of my tests suddently failed!
- I decided to setup a VM. And this took muuuuuuuch longer then I expected....
- but still, some of my tests still fail! GRRR!
- next time I will look at regexp_test.rb, hash_test.rb, open3_test.rb