Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Monday, June 17, 2013

I've finished reading "Refactoring: Ruby Edition"

Since this post, it has taken 17 months because I read it slowly with doing another coding or reading another book.

Fortunately, I have already done most of refactoring examples. But these three examples are not, so that this book is useful for me.

  • Introduce Class Annotation
  • Replace Type Code with State/Strategy
  • Introduce Expression Builder

Monday, June 3, 2013

Install Opscode Chef with Ruby 2.0 on FreeBSD Ports Collection

Opscode Chef version 10.x depends on JSON rubygem between 1.4.4 and 1.7.7. But, there are two points to solve:
  • Ports collection's rubygem-json146 does not support Ruby 2.0
  • The newest version of Ports collection's rubygem-json is 1.8.0
So that we must be some operations for install chef
  1. cd /usr/ports/devel/rubygem-json
  2. Install and do ports-mgmt/portdowngrade to downgrade to 1.7.7
    (or edit Makefile directly to write version 1.7.7)
  3. make install
  4. cd /usr/ports/sysutils/rubygem-chef
  5. edit Makefile
    rubygem-json>=1.4.4:${PORTSDIR}/devel/rubygem-json146 \ to rubygem-json>=1.4.4:${PORTSDIR}/devel/rubygem-json \
  6. make install
At last operation, ports collection fails because of rdoc, but Chef rubygem itself are succeeded.

Thursday, May 30, 2013

The way to install sqlite3 gem on Ruby 2.0 on Windows

Until Ruby 1.9.3, the way to install sqlite3 gem on Windows is easy - just do gem install. But, since Ruby 2.0, it becomes a difficult way. The solution is written in this article, and this is the only one article at this moment.

Saturday, May 18, 2013

Ruby 1.9.3-p429, Ruby 2.0.0-p195, JRuby 1.7.4 had been released

In this week, Ruby 1.9.3-p429, Ruby 2.0.0-p195 and JRuby 1.7.4 had been released. Ruby 2.0.0-p195 is the first release that is stable 2.0.x release on Windows (from here), and JRuby 1.7.4 us the first release that bundles "2.0 mode".
>%JRUBY_HOME%\bin\jruby --2.0 --version
jruby 1.7.4 (2.0.0) 2013-05-16 2390d3b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_21-b11 [Windows 7-amd64]

Thursday, April 18, 2013

Twin prime on Enumerator::Lazy

About twin prime : http://en.wikipedia.org/wiki/Twin_prime
irb(main):001:0> class Enumerator::Lazy
irb(main):002:1>   def at(nth)
irb(main):003:2>     self.drop(nth).first
irb(main):004:2>   end
irb(main):005:1>   def slice(lower, upper)
irb(main):006:2>     self.drop(lower).take(upper - lower + 1)
irb(main):007:2>   end
irb(main):008:1> end
=> nil
irb(main):009:0> require 'prime'
=> true
irb(main):010:0> twin_prime = Prime.lazy.each_cons(2).select{|p,r| r-p == 2}
=> #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: Prime>:each_cons(2)>:select>
irb(main):011:0> twin_prime.at 0
=> [3, 5]
irb(main):012:0> twin_prime.at 1
=> [5, 7]
irb(main):013:0> twin_prime.at 2
=> [11, 13]
irb(main):014:0> twin_prime.at 3
=> [17, 19]
irb(main):015:0> twin_prime.at 100
=> [3851, 3853]

Monday, April 15, 2013

Getting nth and slice for Enumerator::Lazy

Ruby 2.0's Enumerator::Lazy does not have a method for getting nth or getting slice, because Enumerator::Lazy has no indexers whileEnumerable has ones. But, we can same thing defining method on Enumerator::Lazy class.
class Enumerator::Lazy
  def at(nth)
    self.drop(nth).first
  end
  def slice(lower, upper)
    self.drop(lower).take(upper - lower + 1)
  end
end
irb(main):009:0> oneToFive = [1,2,3,4,5].lazy
=> #<Enumerator::Lazy: [1, 2, 3, 4, 5]>
irb(main):010:0> oneToFive.at 1
=> 2
irb(main):011:0> oneToFive.slice 1, 3
=> #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: [1, 2, 3, 4, 5]>:drop(1)>:take(3)>
irb(main):012:0> (oneToFive.slice 1, 3).to_a
=> [2, 3, 4]

Thursday, February 28, 2013

Ruby DevKit is also updated

Ruby DevKit is also updated. There are two distributions.

If you use MinGW in Windows 64bit, you should add configure option --host=x86_64-mingw32 or --build=x86_64-mingw32.

Monday, February 25, 2013

Ruby 2.0.0 has been released

In yesterday, Ruby 2.0.0 has been released, finally.
If you build this tarball on MinGW32, you must do configure with --host=mingw32 option like this post.

Ruby 2.0.0 contains RubyGems 2.0.0, and if you want to use bundler with RubyGems 2.0.0, there are 2 ways.

Wednesday, February 6, 2013

Yield Bang is similar to Ruby's multiple assignment

(In learning from "Programming F# 3.0, 2nd Edition")

Yield Bang ( yield! ) is similar to Ruby's multiple assignment.

In Ruby (multiple assignment)

require 'pathname'

def all_file_under(pathname)
  pathname.each_child.find_all(&:directory?).reduce(pathname.each_child.find_all(&:file?)) do |accum, subpathname|
    accum = Array[*accum, *all_file_under(subpathname)]
  end
end

puts all_file_under(Pathname.new('C:\temp'))
C:\temp/AAAAA
C:\temp/BBBBBB
C:\temp/CCCCCCC
C:\temp/DDD/EEEE
...

In F# (Yield Bang)

> open System.IO;;
> let rec allFilesUnder basePath =
-     seq {
-         yield! Directory.GetFiles(basePath)
-         for subdir in Directory.GetDirectories(basePath) do
-             yield! allFilesUnder subdir
-     };;

val allFilesUnder : basePath:string -> seq<string>

> allFilesUnder @"C:\temp";;
val it : seq<string> =
  seq
    ["C:\temp\AAAAA"; "C:\temp\BBBBBB";
     "C:\temp\CCCCCCC"; "C:\temp\DDD\EEEE"; ...]

Thursday, January 10, 2013

Ruby 2.0.0 rc 1 has been announced

In this week, Ruby 2.0.0 rc 1 has been announced.

[ruby-dev:46847] [ANN] ruby 2.0.0-rc1 released

The most notable point in this release is that "Refinments" has been positioned as "experimental feature".

And in this release, We can build the one for MinGW32 straightforward, in other words, without any MinGW32 options like my past article.

Tuesday, December 11, 2012

Ruby 2.0.0 preview 2 on MinGW32

A week ago, Ruby 2.0.0 preview 2 has been announced.

[ruby-core:50443] [ANN] ruby 2.0.0-preview2 released

But, this release has some tricky points to compile on MinGW32.

  1. patch https://bugs.ruby-lang.org/attachments/3320/configure.in.mingw32_gcc_builtins.patch
  2. configure with --host=mingw32 option

Friday, November 16, 2012

Some libraries for Rails 3 still depend to rake 0.9

In this week, both Rails 3.2.9 and Rake 10.x have been released. Rails itself does not depend to rake version. But, some libraries of Rails 3 are depending to rake 0.9.x. so we should fix Gemfile like below:
gem 'rails', '3.2.9'
gem 'rake', '~> 0.9'

Thursday, November 8, 2012

Ruby 2.0 new feature : Refinements

In previous article, I wrote the ruby code with "monkey-patching". But, since ruby 2.0, there are better way to do it. It is "Refinements".
require 'singleton'

# Fibonacci as Singleton class
class Fibonacci < Enumerator
  include Singleton

  def initialize
    super { |y|
      a = 0
      b = 1
      loop do
        y << a
        a, b = b, a + b
      end
    }
  end
end

# define a refinement
module Nth
  refine Enumerator do
    def nth(n)
      self.take(n+1).find.with_index { |elem, i| i == n }
    end
  end
end

begin
  # there are no additional methods
  puts Fibonacci.instance.lazy.nth(100)
rescue => e
  puts e
end

#using refinments
using Nth

begin
  # there is an additional method
  puts Fibonacci.instance.lazy.nth(100)
rescue => e
  puts e
end

$ cd /c/ruby-2.0.0
$ ./bin/ruby ~/fibonacci.rb
undefined method `nth' for #<Enumerator::Lazy:0x26430d8>
354224848179261915075

Monday, November 5, 2012

Ruby 2.0 new feature : Enumerator::Lazy

In functional language, The feature called "lazy evaluation" exists as its key feature. For example, fibonacci in F# is:
Microsoft (R) F# Interactive version 11.0.50727.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> let fibonacci = Seq.cache <| Seq.unfold (fun (current, next) -> Some(current, (next, current + next))) (0I, 1I);;

val fibonacci : seq

> fibonacci |> Seq.nth 100;;
val it : System.Numerics.BigInteger =
  354224848179261915075 {IsEven = false;
                         IsOne = false;
                         IsPowerOfTwo = false;
                         IsZero = false;
                         Sign = 1;}

Ruby 2.0's Enumerator::Lazy can do "lazy evaluation" like this:
irb(main):001:0> RUBY_VERSION
=> "2.0.0"
irb(main):002:0> fibonacci = Enumerator.new { |y|
irb(main):003:1*   a = 0
irb(main):004:1*   b = 1
irb(main):005:1>   loop do
irb(main):006:2*     y << a
irb(main):007:2>     a, b = b, a + b
irb(main):008:2>   end
irb(main):009:1> }.lazy
=> #<Enumerator::Lazy: #<Enumerator: #<Enumerator::Generator:0x23cbbc0>:each>>
irb(main):010:0> def fibonacci.nth(n)
irb(main):011:1>   self.take(n+1).find.with_index { |elem, i| i == n }
irb(main):012:1> end
=> nil
irb(main):013:0> fibonacci.nth(100)
=> 354224848179261915075

If the above sample did not use Enumerator::Lazy, it would take many many seconds.

Friday, November 2, 2012

Ruby 2.0.0 preview 1 has been announced

Today, Ruby 2.0.0 preview 1 has been announced.

[ruby-dev:46348] [ANN] ruby 2.0.0-preview1 released

On my Windows (MinGW), build goes green.

Saturday, September 29, 2012

Calculate the firefox year (Fixed)

(Check also phosphorescence: Calculate the firefox year)

Firefox team announced changing their "Rapid release" plan.

The reason is "the Happy Holidays". BTW, I fix previous "Calculate the firefox year" ruby code for this change.
$ irb -rfiber -rdate
irb(main):001:0> next_firefox = Fiber.new do
irb(main):002:1*   ff5 = Date.new(2011, 6, 21)
irb(main):003:1>   ff_next = 6
irb(main):004:1>   ff_next_date = ff5 + 7 * 8
irb(main):005:1>   loop do
irb(main):006:2*     Fiber.yield ff_next, ff_next_date
irb(main):007:2>     ff_next += 1
irb(main):008:2>     ff_plan_date = ff_next_date + 7 * 6
irb(main):009:2>     ff_happy_holiday = Date.new(ff_next_date.year, 12, 25)
irb(main):010:2>     if ff_happy_holiday.year >= 2012 && (ff_next_date..ff_plan_date).include?(ff_happy_holiday)
irb(main):011:3>       ff_next_date = ff_plan_date + 7
irb(main):012:3>     else
irb(main):013:3*       ff_next_date = ff_plan_date
irb(main):014:3>     end
irb(main):015:2>   end
irb(main):016:1> end
=> #<Fiber:0x2dc0af8>
irb(main):017:0> next_firefox_version, next_firefox_date = next_firefox.resume
=> [6, #<Date: 2011-08-16 ((2455790j,0s,0n),+0s,2299161j)>]
irb(main):018:0> while (next_firefox_version < next_firefox_date.year) do
irb(main):019:1>   next_firefox_version, next_firefox_date = next_firefox.resume
irb(main):020:1> end
=> nil
irb(main):021:0> puts "The version equals release year", next_firefox_version, next_firefox_date
The version equals release year
2277
2277-11-06
=> nil

Monday, September 24, 2012

Euler's formula does not equal because of Float in Ruby

Euler's formula does not equal because of Float in Ruby. Let's show the example at x = Π.

$ irb -rcmath
irb(main):001:0> include CMath
=> Object
irb(main):002:0> left_euler = exp(-1.0*sqrt(-1.0)*PI)
=> (-1.0-1.2246063538223773e-16i)
irb(main):003:0> right_euler = cos(PI) + sqrt(-1.0) * sin(PI)
=> (-1.0+1.2246063538223773e-16i)
irb(main):004:0> left_euler == right_euler
=> false
irb(main):005:0> left_euler === right_euler
=> false

(check also phosphorescence: Euler's formula in Ruby)

Friday, September 21, 2012

UUID in Ruby

There are 3 ways to deal UUID in Ruby.

(1) Use SecureRandom class

Check my article.

(2) Install uuid gem

Check Github page of uuid gem.

(3) [for JRuby] Using java.util.UUID

irb(main):001:0> require 'java'
irb(main):002:0> import java.util.UUID
irb(main):003:0> UUID.randomUUID.to_s
=> "e42ccfce-d6a0-4cb5-a0c0-9fae6ca05b84"
irb(main):003:0> UUID.randomUUID.to_s
=> "80a31bb3-3fce-4e6f-9c8a-5a819de45960"

Saturday, August 18, 2012

Agile Web Development with Rails 4th edition has been updated on recent rails version

Agile Web Development with Rails 4th edition has been updated on recent rails version from 3.2.0 to 3.2.6:

The notable point is that Rails 3.2.6 is the first version support attr_accessible setting as default.

But, The file upload way on chapter 21 is still older one. See my post : phosphorescence: Sample for uploading file in Rails3 way on AWDwR 4th chapter21.

Thursday, August 2, 2012

SecureRandom

SecureRandom class is the facade class to issue base64, uuid and so forth.
[1] pry(main)> require 'securerandom'
=> true
[2] pry(main)> SecureRandom.hex
=> "444adf4fe82c00ab6d9a7cdffc6d46e1"
[3] pry(main)> SecureRandom.base64
=> "B4hdEsaM3icerGi2/LIEFw=="
[4] pry(main)> SecureRandom.urlsafe_base64
=> "--gOBG02VkWhimQz-6mVyA"
[5] pry(main)> SecureRandom.uuid
=> "da3488c8-0aca-45d0-bd60-d2d1a1df1a02"