Forum: Ruby Print Out Code in Program

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
52570ba02b961e34514cc22bfd1de50d?d=identicon&s=25 Patrick (Guest)
on 2006-05-09 06:40
class Ab
def code
a = 5
b = 2
end
end

Is there a way to store the actual code of a class or definition as a
string without manually doing it? Such as if you were doing a tutorial
on how something works. So basically I could do print aB and I get "def
code a = 5 b = 2 end"
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-09 06:53
(Received via mailing list)
On May 9, 2006, at 12:40 AM, Patrick wrote:

> "def
> code a = 5 b = 2 end"
>
> --
> Posted via http://www.ruby-forum.com/.
>

Sounds like a job for xmp[1]
[1] http://raa.ruby-lang.org/project/xmp/
52570ba02b961e34514cc22bfd1de50d?d=identicon&s=25 Patrick (Guest)
on 2006-05-09 07:33
Thanks, the code works, but is there still a way to print out everything
in a class by just referencing the class name? To get everything to be
displayed using this code it has to basicaly start before the class and
end afterward.
5d15f83f0e681b138d95f1ec430868fa?d=identicon&s=25 Joey (Guest)
on 2006-05-09 09:14
(Received via mailing list)
@ab = <<-CODE
class Ab
def code
a = 5
b = 2
end
end
CODE

eval(@ab)
def print_class(klass)
  print instance_variable_get "@#{klass.to_s.downcase}"
end

print_class(Ab)
D8fb06dfc08a477ecb0a76ffdbff3475?d=identicon&s=25 Chiaro Scuro (chiaroscuro)
on 2006-05-09 11:58
(Received via mailing list)
I was asking something similar about blocks in the near past.  After a
fiery discussion I was advised tou use a ParseTree trick to go back to
the original code and read it..
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 gene.tani@gmail.com (Guest)
on 2006-05-09 15:43
(Received via mailing list)
Patrick wrote:
> code a = 5 b = 2 end"
>

SCRIPT_LINES__, maybe?
http://www.rubyquiz.com/quiz38.html
This topic is locked and can not be replied to.