Posted: January 12th, 2010 | Author: Spoofy | Filed under: Brain Dump | Tags: braindump, PHP, ruby | Comments Off
Today I had to dig into some old code of mine for a project, which tends to happen, however the archival code was in PHP but I needed it in Ruby. So, to borrow a term from the pro-audio and encoding industry, I transcoded PHP to Ruby (is there a programming equivalent of this word?) using Notepad++ and some regular expressions.
This is far from complete, but it might be a good starting place if you want to convert a PHP project to Ruby:
1. strip < ?php ?> tags at start/end of script
2. replace function with def
3. replace } with end
4. remove {
5. replace // with #
6. replace /*…*/ with #
7. replace null with nil
8. replace $this-> with @
9. replace \$([a-zA-Z0-9]+) with \1
10. replace (\)\w*;) with )
11. remove ;$
12. replace -> with .
13. replace is_string\(([a-zA-Z0-9]+)\) with \1.is_a?String
14. replace is_array\(([a-zA-Z0-9]+)\) with \1.is_a?Array
15. replace else if with elsif
16. replace new\W*([a-zA-Z0-9]+)\W*\( with \1.new(
17. replace .= with +=
18. replace strlen\((@*[a-zA-Z0-9]+)\) with \1.length
19. replace count\((@*[a-zA-Z0-9]+)\) with \1.length
20. replace \W*instanceof\W* with .is_a?
21. replace strtolower\(([a-zA-Z0-9\[\]@\.=’,\(\)]+)\) with \1.downcase
Posted: December 20th, 2009 | Author: Spoofy | Filed under: Articles, Programming | Tags: bug, Programming, ruby, variable-scope | Comments Off
I recently began work on a Twitter library for Ruby, some already exist -which I didn’t consider, I wanted to develop a library of my own design which took a very object-oriented approach to accessing Twitter, mostly for practice. Because I wanted an intuitive, and for the lack of a better word, ‘logical’ implementation, I decided to use nested classes.
Ruby nested classes are child classes which are defined within a parent class. Unbeknownst to me, Ruby has some interesting quirks when using nested classes. Take for example, a typical class:
class A
attr_accessor :var
def initialize
@var = "something interesting"
end
def tellme
puts @var
end
end
Nothing peculiar here, but lets say we want a child class – for all intents and purposes, a namespace, which doesnt inherit from its parent but does exist inside it:
class A
attr_accessor :var
def initialize
@var = "something interesting"
end
def say
puts @var
end
class B
def tellme
puts @var
end
end
end
What we’re doing here is creating a parent class, A; inside that class we have another class B which is a part-of class A, but is not an inherited child. This is an important distinction, because an inherited child would have their own instance variables – what we have here is a class, A which ‘owns’ B. In theory, B should have access to A’s variables.
Heres a usage example, which you might expect to work:
parent = A.new
parent.tellme
child = A::B.new
child.tellme
In theory, we should have the same output, ‘something interesting‘ from calling the tellme method on each class, but no. Instead the child will throw an exception, why? because the instance variable is not visible – it is outside its variable scope. To reiterate: nested classes are outside the variable scope of their parent class.
This is true of class variables and constants. Nested children of a parent class simply do not have access to their parents, unless you provide that functionality programmatically:
class A
attr_accessor :b, :var
def initialize
@var = "something interesting"
@b = B.new(self)
end
def say
puts @var
end
class B
def initialize(theparent)
@parent = theparent
end
def tellme
puts @parent.var
end
end
end
a = A.new
a.tellme
a.b.tellme
This is the solution I ended up using, hopefully this post will have helped you avoid some of the frustration I went through trying to figure it out.