From: Robert Dober Date: 2007-07-29T16:35:24+09:00 Subject: Re: how can i use a ruby class in object way On 7/29/07, Guo Yangguang wrote: > thank you very much!Your answer make me understand more about object in > ruby.I am very glad.But still ,i have another question.Please read an > example following: > class Test > def initialize > e="e" > end > def Test.say_hello > puts "Hello from #{self.name}" > puts "hello from #{self.class}" > end > say_hello > end > It is not completely clear what you want to achieve but some remarks might be helpful class Test def initialize e="e" # this code is not realy useful, e is a scope local variable that will be thrown away, you probably want to do this: @e = "e" # this is an instance variable end attr_accessor :e # for some tests below def Test.say_hello ... end say_hello end Test.say_hello aTest = Test.new puts aTest.e aTest.e = 42 puts aTest.e > > > > FireAphis wrote: > > On Jul 26, 6:43 am, Guo Yangguang wrote: > >> -- > >> Posted viahttp://www.ruby-forum.com/. > > > > I hope I understand your question correctly. > > > > Ruby is in its essence an interpreter. When you type in your command > > prompt > > > >>ruby my_script.rb > > > > the interpreter (that by itself is just another application) reads > > your file line by line and executes every line. Think of every line in > > Ruby as a command to the interpreter and that the interpreter executes > > every single line. That means you can use your classes anywhere you > > like as long as they were defined before. For example, this is > > absolutely correct in Ruby: > > > > a = "I assign even before any declaration" > > puts "Moreover I can output its value #{a}" > > > > (1..10).each {|x| puts "#{x} I can do anything I wish"} > > > > class C1 > > puts "When interpreter will read this he will print. Not very > > useful but possible" > > def a() > > end > > end > > > > c = C1.new # now we can use our class > > puts "We can do something in between..." > > > > class C2 > > puts "We can define another class" > > def b > > end > > end > > > > puts "And so on..." > > > > > > As you can see it's quite different from Java. If you want to feel > > more comfortable think of your Ruby code written entirely inside one > > big 'main' Java function. Just remember - every line is executed! > > > > FireAphis > > -- > Posted via http://www.ruby-forum.com/. > > -- [...] as simple as possible, but no simpler. -- Attributed to Albert Einstein