-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.txt
More file actions
81 lines (69 loc) · 5.07 KB
/
examples.txt
File metadata and controls
81 lines (69 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
;;Basic Operations
;ruby declare x, y; x = 1; y = 9; puts (x+(y+1)); end --> 11
;ruby declare x, y, z; x = 1; y = 9; z = 4; puts (z+(x*(y+1))); end --> 14
;ruby declare x, y, z; x = 1; y = 9; z = 4; puts ((z+(x*(y+1)))/2); end --> 7
;ruby declare x, y, z, w; x = 1; y = 9; z = 4; w = ((z+(x*(y+1)))/2); puts w; end --> 7
;ruby puts("hola"+"mundo"); end --> "holamundo"
;ruby puts("hola"*4); end --> "holaholaholahola"
;ruby puts("hola"*1); end --> "hola"
;ruby puts("hola"*0); end --> ""
;;For examples
;;ruby for x in (1..10) do puts x; end end
;;ruby for x in (1...10) do puts x; end end
;;For con step
;;ruby for x in ((1..9) step 2) do puts x; end end
;;While examples
;;ruby declare x; x = 0; while(x < 10) do puts x; x = (x+1); end end
;;ruby declare x; x = 0; while(x < 10) do x = (x+1); puts x; end end
;;ruby declare x; x = 0; while(x < 10) do x +=1 ; puts x; end end
;;Until Examples
;;ruby declare x; x = 0; until (x >= 10) do puts x; x = (x+1); end end
;;If examples
;;ruby if (1 == 1) then puts 1; else puts 0; end end
;;ruby declare n; n = 2; if (n == 1) then puts "A"; elsif (n == 2) puts "B"; else puts 0; end end
;;ruby if (2 == 1) then puts "A"; elsif (2 == 2) puts "B"; else puts 0; end end
;;ruby if (2 == 1) then puts "A"; elsif (2 == 6) puts "B"; elsif (3 == 3) puts "C"; else puts 0; end end
;;ruby declare n; n = 3; if (n == 1) then puts "A"; elsif (n == 2) puts "B"; elsif (n == 3) puts "C"; elsif (n == 4) puts "D"; else puts 0; end end
;;ruby declare n, x; n = 6; if (n > 7) then x = 1; puts x; else n = 9; puts n; end end
;;ruby declare x; x = 3; if (x > 5) then puts x; elsif (x < 3) then puts "mundo"; else puts "hola"; else puts "otro else"; end end
;;ruby if(true && false) then puts true; else puts false; end end
;;ruby if(true || false) then puts true; else puts false; end end
;;Unless Examples
;;ruby unless false then puts 4; end end
;;ruby unless true then puts 4; else puts "hola mundo"; end end
;;ruby unless true then puts 4; else puts "hola mundo"; else puts "otro else"; end end
;;assigns-and examples
;;ruby def add1(val) val+=1; return val; end add1(100); end
;;ruby declare x; x = 10; x += 2; puts x; end
;;ruby declare x; x = 10; x -= 2; puts x; end
;;ruby declare x; x = 10; x /= 2; puts x; end
;;ruby declare x; x = 10; x **= 2; puts x; end
;;Procedures examples
;;ruby def print(val) puts val; end print("hola mundo"); end
;;ruby def add1(val) val += 1 ; return val; end add1(100); end
;;ruby def fact(n) if (n == 0) then return 1; else puts "el valor de n es: ", n; return (n*(fact((n - 1)))); end end puts "el factorial de 5 es: ",(fact(5)); end
;;ruby def to_text(num) declare text; text = ["cero", "uno", "dos", "tres"]; return (text[num]); end to_text(3); end
;; ruby def mundo() return("mundo"); end puts("hola"+(mundo())); end
;;ruby def mundo() return(1); end puts(2+(mundo())); end
;;ruby def mundo() return(true); end puts(true and (mundo())); end
;;ruby def fibo(n) if (n <= 2) then return 1; else declare left, right; left = (n - 1); right = (n - 2); return ((fibo(left))+(fibo(right))); end end puts(fibo(10)); end
;;ruby def f() declare text; text = ["cero", "uno", "dos", "tres"]; return (text); end f()[0]; end
;;ruby def f(j) if (j > 4) then return 3; else return 10; end end f(9); end
;;ruby def f(j) if (!(j > 4)) then return 3; else return 10; end end f(9); end
;;ruby def intervalo(min,max,value) if (min < value) then if (value < max) then return true; end end return false; end puts(intervalo(0,10,5)); end
;;ruby def intervalo(min,max,value) if (min < value) then if (value < max) then return true; end end return false; end puts(intervalo(0,10,15)); end
;;Array examples
;;ruby declare a; a = [1, 2, 3, 4, 5]; a[3]; end --> 4
;;ruby declare a; a = [1, 2, 3, 4, 5]; a[0,2]; end --> (1 2)
;;ruby declare x; x = [[1,2,3],4,5,6]; x[0]; end --> (1 2 3)
;;ruby declare x; x = [[1,2,3],4,5,6]; x[0][0]; end --> 1
;;ruby declare x; x = [[1,2,3],4,5,6]; x[2]; end --> 5
;;ruby declare x; x = [[1,2,3],4,5,6]; x[0][2]; end --> 3
;;ruby declare x; x = [[1,2,3],4,5,6,[7,8,9]]; x[4]; end --> (7 8 9)
;;ruby declare x; x = [[1,2,[3,4]],5,6]; x[0][2][0]; end --> 3
;;Multiple elements example
;;ruby declare a, b; b = [0, 10, 100]; a = [1, 2, 3, 4, 5]; for x in (0..4) do puts(a[x]); end puts b; end
;;ruby def f(n,m) declare z; n+=10; z = n; m*=(z+2); return m; end if((f(1,2)) > 4) then puts "hola"; else "mundo"; end end
;;Objects example
;;ruby class Vector2D attr :x, :y; def initialize(x,y) @x = x; @y = y; end def x() return @x; end def x=(x) @x = x; end def y() return @y; end def y=(y) @y = y; end end class Vector3D < Vector2D attr :z; def initialize(x,y,z) @z = z; super initialize(x,y); end def z() return @z; end def z=(z) @z = z; end end declare a,b; a = Vector2D.new(1,2); b = Vector3D.new(3,4,5); puts (a.x()); puts(b.z()); end
;;ruby class Integer attr :num; def initialize(num) @num = num; end def num=(num) @num = num; end def to_s() return @num; end end class Float < Integer attr :decm; def initialize(ent,decm) @decm = decm; super initialize(ent); end def to_s() return (@num + (@decm / (@decm * 10))); end end declare a; a = Float.new(1,1); puts(a.to_s()); a.num=(5); puts(a.to_s()); end