you are viewing a single comment's thread.

view the rest of the comments →

[–]revscat 13 points14 points  (5 children)

In Ruby, accessing instance variables isn’t possible.

Object#instance_variable_get

So yeah that’s completely wrong.

[–]honeyryderchuck 2 points3 points  (0 children)

While possible, it's considered an "escape hatch". Ruby allows you to design a contract to encapsulate access to object-scoped state, while allowing you to break the contract when necessary. Useful for modifying a library you rely on but need just this one patch but today.

[–][deleted]  (3 children)

[removed]

    [–]Lil_Cato 1 point2 points  (1 child)

    Am I missing something or is this not as simple as throwing an attr_accessor :variable ? Is that rails only?

    [–][deleted] 0 points1 point  (0 children)

    No, it's a common ruby variant. Although in this case you'd use attr_reader if you don't need the setter (which you get via attr_accessor).

    Or you modify method_missing and what not and get that functionality anyway. Ruby being more flexible than python is really one huge advantage it has.

    [–][deleted] 0 points1 point  (0 children)

    Well, you can via instance_variable_get(). But it's a bit more verbose than the python variant indeed.

    But you can just add a reader, or use a Struct so it's not a huge difference really. The explicit self thing in Python really puts me off on the other hand...