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

Wednesday, February 18, 2015

DZone shortest code to print first 30 fibonacci numbers without using numbers

There's interesting challenge on Dzone website. The task is to print Fibonacci sequence for first 30 numbers in the format "order: fib number". Once constraint is that you can't use any numbers in the code. Shortest answer wins.
My take on that problem in Ruby is here (85characters):
[edit] During morning run I've realised that using array is just PITA and by getting rid of indexes for last and previous elements I can save some space (64 characters):
Code snippet - 30 fibonacci numbers without using numbers in Ruby on Snipplr

[edit] Since there was groovy solution with 64 characters I've shaved off another two getting down to 62.
Code snippet - 30 Fibonacci Numbers Without Using Numbers In Ruby(62) on Snipplr

Later when I read discussion they have dropped requirement to use : in the output so I decided to update solution and found another nifty trick to get rid of another two characters ending up at 58(60) chars which can be easily reduced to 57 by removing whitespace after p.

Friday, April 11, 2014

ENV is not a Hash but Object in Ruby

Recently I hit this funny problem in ruby, when trying to merge options to ENV. Although ENV supports most of Hash functions, it's not really Hash instance:
irb(main):001:0> ENV.class
=> Object
irb(main):002:0> env = { 'X'=>1 }
=> {"X"=>1}
irb(main):003:0> env.class
=> Hash
There are some useful methods missing from ENV like merge!
irb(main):004:0> ENV.merge(env)
NoMethodError: undefined method `merge' for #<object:0xb7f54814>
       from (irb):4
Workaround is simple enough, merge! hash manualy:
irb(main):012:0> env.each { |k,v| ENV[k.to_s]=v.to_s }
=> {"X"=>1}
irb(main):013:0> ENV["X"]
=> "1"

Wednesday, April 9, 2014

Work around core library class mocks/stubs in ruby

Just a short post about mocking/stubbing your Ruby functions. Let's have a class like this:
class ConfigurableClass
  attr_reader :option
  def configure_from_file(file)
    json = JSON(IO.read(file))
    @option = json['my']['option']
  end
end

One way to test this would be to mock IO.read with Mocha
describe ConfigurableClass do
 it "can configure from json" do
   data =  '{ "my": { "option" : "hello" } }'
     IO.any_instance.expect(:read).with("x.json").returns(data)
     cc = ConfigurableClass.new
     cc.configure_from_file("x.json")
     cc.option.should eq "hello"
   end
end

This is tricky if your code does IO.read anywhere else which you obviously don't want to mock. However with some refactoring code is much cleaner.
class ConfigurableClass
  attr_reader :option
  def read_config_file(file)
    IO.read(file)
  end

  def configure_from_file(file)
    json = JSON(read_config_file)
    @option = json['my']['option']
  end
end

Then in the rspec it's easy to mock only function specific to ConfigurableClass.
describe ConfigurableClass do
  it "can configure from json" do
    data =  '{ "my": { "option" : "hello" } }'
    ConfigurableClass.any_instance.expect(read_config_file).with("x.json").returns(data)
    cc = ConfigurableClass.new
    cc.configure_from_file("x.json")
    cc.option.should eq "hello"
end

Friday, March 7, 2008

Ruby documentation

I found The Pragmatic programmers guide to Ruby most usefull. Also I printed out Small Ruby reference leaflet. You can find very nice Ruby tutorial at rubylearning.com.

Wednesday, March 5, 2008

Things I hatedon't like about Ruby

Ruby is quite cool, I like lot of features, but for me as C/C++ developer there are some things, which are quite annoying. Please note, that I'm aware that lot of issues I mention here are actually Ruby features. This post will be dynamic, so more items will be eventualy added. For each issue, I'll try to search for solution or workaround. Also I encourage you to post any ideas you might have about solving these problems.

  • multiple classes per file
    I know that you can have more classes in C++ as well, but I like Java way. I also know that Ruby doesn't force you into writing multiple classes per file, but it also doesn't discourage people that write spaghetti code. If you end up maintaining project like this, it's hell.
    Solution: Use search to navigate project, or refactor to class per file.

  • variables doesn't have to be declared
    This is anoying, if you have to study some old code. In Java/C++ world you have to declare class/object variables. If you look at the code after some time you have all members of the class neatly organised at declaration section. In ruby you're free to use variable wherever you like, without first declaring it.
    Solution: correct way of using members is to use accessors: attr_reader, attr_accessor, and attr_writer.
    Well ... one friend had good note: "Always code, as if the guy which end up maintaining your code is violent psychopat, who knows where you live."

  • arguments lack type information
    This is related to previous item. Coding with strongly type-based languages is quite addictive. Switching to Ruby, you'll start missing those: "cannot cast x to y" errors from compiler.
    Solution: ?

Wednesday, February 27, 2008

RDT - IDE for Ruby development

I've been assigned to a project which is developed in Ruby. Because I'm not familliar with Ruby, I decided to use some IDE to work on the project. Because i like Eclipse for java/c++ development, my first choice was to look for Eclipse based IDE. Quick google search returned RDT - or rubyeclipse. Of course I've also bookmarked Ruby Tutorial. I'll post more after I play with it a little bit.