Parte imperative
L'ultima righa del programma seguente dovrebbe restituire true
class Test
def Test.isPrime n
if (n < 2)
return false
end
if (n == 2)
return true
end
if (((n / 2) * 2) == n)
return false
end
a = 3
loop do
if (n < a * a)
return true
end
if (((n / a) * a) == n)
return false
end
a = a + 2
end
end
end
Test.isPrime(17)
Chiamata di metodi
L'ultima righa del programma seguente dovrebbe restituire 1
class A
def method
return 1
end
end
class B
def method
return A.new
end
end
B.new.method.method
Ridichiarazione di metodi
L'ultima righa del programma seguente dovrebbe restituire 3
class Test
def method1
return 1
end
end
a = Test.new
b = a.method1
class Test
def method1
return 2
end
end
b = b + a.method1
Ereditarietà
L'ultima righa del programma seguente dovrebbe restituire 6
class A
def method1
return 1
end
end
class B < A
def method1
return 2
end
def method2
return 3
end
end
(A.new.method1) + (B.new.method1) + (B.new.method2)
Variabile locale
L'ultima righa del programma seguente dovrebbe restituire 3
class Test
def method1
a = 2
return a
end
def method2
a = 3
b = method1
return a
end
end
Test.new.method2
Campo
L'ultima righa del programma seguente dovrebbe restituire 2
class Test
def method1
@a = 2
return @a
end
def method2
@a = 3
b = method1
return @a
end
end
Test.new.method2
Campo Statico
L'ultima righa del programma seguente dovrebbe restituire 3
class Test
@@a = 0
def method1
@@a = @@a + 1
@a = @@a
return @a
end
def method2
b = method1
return @a
end
end
puts (Test.new.method2) + (Test.new.method2)
Parametri
L'ultima righa del programma seguente dovrebbe restituire 5
class Test
def method1(n)
n = 3
return n
end
def method2
n = 2
m = method1(n)
return n + m
end
end
Test.new.method2
Chiusura
L'ultima righa del programma seguente dovrebbe restituire 11
class A
def twice(n)
yield(n)
yield(n+1)
end
def test
a = 0
twice(5) do |m|
a = a + m
end
a
end
end
A.new.test