ActionScript 3 to Ruby Examples

Written by

I have been avidly attempting to learn Ruby and to begin with, found writing out simple code comparisons for each language helped me visualise how Ruby came together better. I thought these may prove useful to some other AS3 devs looking to have a play around with Ruby so am sharing them in this blog post. I will aim to update them over time and welcome input and suggestions. There are many resources out there for learning Ruby but for a quick overview I found Lukes post and Ruby in 20 minutes were great!

ActionScript to Ruby Tips

hello.as
1
private var name:String="Simon";
hello.rb
1
@name="Simon"
hello.as
1
2
var name:String="Simon";
trace("Hello "+name);
hello.rb
1
2
name="Simon"
puts "Hello #{name}"
hello.as
1
2
3
4
5
6
function dump(name:String="World")
{
    trace("Hello "+name);
}

dump("Simon");
hello.rb
1
2
3
4
5
def dump(name="World")
    puts "Hello #{name}"
end

dump "Simon"
hello.as
1
2
3
4
5
var _names:Array=["tom", "dick", "harry"];
for(var name:String in _names)
{
    trace("Hello "+name);
}
hello.rb
1
2
3
4
names=["tom", "dick", "harry"]
names.each do |name|
    puts "Hello #{name}"
end
hello.as
1
2
var _names:Array=["tom", "dick", "harry"];
trace("Hello "+_names.join(", "));
hello.rb
1
2
names=["tom", "dick", "harry"]
puts "Hello #{names.join(", ")}"
hello.as
1
2
3
4
5
6
7
8
9
10
11
12
13
var _names:Array=["tom", "dick", "harry"];
if(_names==null)
{
    trace("nil");
}
else if(!_names.length)
{
    trace("empty");
}
else
{
    trace("Hello "+_names[0]);
}
hello.rb
1
2
3
4
5
6
7
8
names=["tom", "dick", "harry"]
if names.nil?
    puts "nil"
elsif names.empty?
    puts "empty"
else
    puts "Hello #{names[0]}"
end
hello.as
1
2
3
4
5
6
7
8
9
10
private var _foo:String;
public function set foo(value:String):void
{
    _foo=value;
}

public function get foo():String
{
    return _foo;
}
hello.rb
1
attr_accessor :foo
hello.as
1
2
3
4
5
6
7
8
9
public function set foo(value:String):void
{
    _foo=value;
}

public function get foo():String
{
    return _foo;
}
hello.rb
1
2
3
4
5
6
7
def foo=(value)
    @foo = value
end

def foo
    @foo
end

Comments