leo wrote:
> Any one out there, who speaks ruby and can translate tests, for which
> we have a '.pl' file?.
just catched up mail and thought a bit practice might not be bad...
i intentionally did not wrote them optimal (performance-wise), but rather
elegantly (sort of ;).
thus this is only a performance of comparision of the language
implemenentation not the quality of the VM.
i thought this was kind of wanted (because we may have a ruby frontend one
day).
so have fun...
but dont be overly enthusiastic, ruby is kind of slow... (especially with
blocks)
~ibotty
#! ruby
def addit(*a)
res = 0.0
a.each { |x|
res += x.to_f
}
res
end
arg0 = 1000;
arg1 = 7.100;
arg2 = 87;
arg3 = "3.87";
arg4 = "21000000";
result = 0
500000.times {
result = addit(arg0, arg1, arg2, arg3, arg4)
}
puts result
#! ruby
k = Array.new
nk = Array.new
ha = Hash.new
10.times { |i|
s = (65 + i).chr
k.push s
}
(0..3).each { |e|
10.times { |i|
k.each{ |s|
_nk = s + (65 + i).chr
nk.push _nk
}
}
k = nk
nk = Array.new
}
puts k.length
j = 0
k.each{ |s|
j+=1
ha[s] = 1
}
puts j
puts ha.keys.length
print ha["AAAAA"]
print ha["ABCDE"]
print ha["BBBBB"]
print ha["CCCCC"]
print ha["HHHHH"]
print ha["IIIII"]
puts
#! ruby
#
# this algorithm is so dumb!
# it even hurts to write this equivalent to fib.pl
def fib(n)
return n if (n < 2)
return fib(n - 1) + fib(n - 2)
end
N = Integer( ARGV.shift || 24 )
puts "fib(#{N}) = #{ fib(N) }"
#! ruby
#
# does the perl variant count as oo?
#
class Foo
attr_reader :i, :j
def initialize()
@i = 10
@j = 20
end
end
(1..100000).each{ o = Foo.new }
o = Foo.new
puts o.i
#! ruby
class Foo
attr_reader :i, :j
def initialize()
@i = 10
@j = 20
end
end
(1..500000).each { o = Foo.new }
o = Foo.new
puts o.i
#! ruby
i6 = 0
i7 = 0
#max = 50000
max = 10000
def is_prime(n)
if n < 1
return false
end
(n-1).downto(2) { |i|
if n % i == 0
return false
end
}
true
end
max.times { |i|
if is_prime i
i6 += 1
i7 = i
end
}
puts "# of primes calculated to #{max}: #{i6}"
puts "last is: #{i7}"
#! ruby
def buildarray()
foo = Array.new
10000.times { |i|
foo[i] = i
}
foo
end
a1 = Array.new
10.times { |i|
a1[i] = buildarray
}
a2 = Array.new
10.times { |i|
a2[i] = buildarray
}
a3 = Array.new
10.times { |i|
a3[i] = buildarray
}
#! ruby
def buildarray()
foo = Array.new
10000.times { |i|
foo[i] = i
}
foo
end
20.times {
a = Array.new
10.times { |i|
a[i] = buildarray
}
}
#! ruby
big = 0
string = "just another ruby hacker"
3000000.times {
big += 1
str = string.split(//)
f = str.shift
str.push f
string = str.join('')
}
puts big, string