hello:
can you help me ?we all know that we can use a class’object in main
method in java,but how can i use a class’object after designing a class
in ruby? i only know i can make an object and use it after having a
class,i can also use a class method in a class to get some result after
designing a class method.But why i can do that? i do not know the
reason? is there an only place like main method of java in which i can
use my classes? is there a rule that can guide me to use my ruby class
correctly?
thank you !
2007/7/26, Guo Y. [email protected]:
hello:
can you help me ?we all know that we can use a class’object in main
method in java,but how can i use a class’object after designing a class
in ruby? i only know i can make an object and use it after having a
class,i can also use a class method in a class to get some result after
designing a class method.But why i can do that? i do not know the
reason? is there an only place like main method of java in which i can
use my classes? is there a rule that can guide me to use my ruby class
correctly?
As has been mentioned already, the body of the script file is what in
Java is your “main” method. Arguments are in ARGV, i.e. you can do
ARGV.each do |arg|
puts arg
end
You can as well read from ARGV - which is a kind of IO that combines
all file names on the command line. A simple “cat” replacement looks
like this:
ARGV.each do |line|
puts line
end
Kind regards
robert
On Jul 26, 6:43 am, Guo Y. [email protected] 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
On 7/26/07, Robert K. [email protected] wrote:
Small typo here
ARGV.each do |line|
ARGF
puts line
endKind regards
robert
Robert
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
Your means is we can use a class anyplaces after defining it ,but in
this example,i use a classmehod immediately after defining it in class
context.This is valid.Can you explain it for me?What is more,if it is a
instance method ,i can not get any result.why?
thank you !
FireAphis wrote:
On Jul 26, 6:43 am, Guo Y. [email protected] 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
promptruby 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
endc = 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
endputs “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
On 7/29/07, Guo Y. [email protected] wrote:
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
Sorry ,i am not show it very clear.Please read this example again:
class Test
def Test.say_hello
puts “Hello from #{self.name}”
puts “hello from #{self.class}”
end
say_hello #i don’t understand here
end
Normally,we can use a class anyplaces after defining it ,but in this
example,i use a classmehod immediately after defining it “in class
context”.This is valid.I means calling a classmethod(not an intance
method) in a class defintion–not after defining that class–is valid
.Can you explain it for me? thank you !
Robert D. wrote:
On 7/29/07, Guo Y. [email protected] wrote:
end
say_hello
endIt is not completely clear what you want to achieve but some remarks
might be helpfulclass 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
endattr_accessor :e # for some tests below
def Test.say_hello
…
end
say_hello
endTest.say_hello
aTest = Test.new
puts aTest.e
aTest.e = 42
puts aTest.e
On 29.07.2007 09:30, Robert D. wrote:
On 7/26/07, Robert K. [email protected] wrote:
Small typo hereARGV.each do |line|
ARGF
puts line
end
Right. Thanks for catching the copy & paste error!
Kind regards
robert
On 7/29/07, Guo Y. [email protected] wrote:
Sorry ,i am not show it very clear.Please read this example again:
class Test
def Test.say_hello
puts “Hello from #{self.name}”
puts “hello from #{self.class}”
end
say_hello #i don’t understand here
end
Try this on for size:
class Test
puts “Self is”
p self
puts “Test is”
p Test
puts “Test == self?”
p( Test == self )
def Test.say_hello
puts "Hello"
end
say_hello
Test.say_hello
self.say_hello
end
In the body of a class block, self is the class. That’s why you
can
call the “class” method right there, after defining it.
Normally,we can use a class anyplaces after defining it ,but in this
OK,i can understand now.Thank you!Thank all the people who helped
me.Unlike our Chinese help site,here is so kind to fresher of ruby.I
will persuade my friends to come here too.
On 7/29/07, Guo Y. [email protected] wrote:
Sorry ,i am not show it very clear.Please read this example again:
class Test
def Test.say_hello
puts “Hello from #{self.name}”
puts “hello from #{self.class}”
end
say_hello #i don’t understand here
ok I see now, well this is “simple” once you have grasped the concept,
but quite difficult before – as so often
It might be helpful to grasp the concept of self before, I’d advice
Pickaxe for that Programming Ruby: The Pragmatic Programmer's Guide
now in Ruby there is always a self defined, the implicit receiver to
which unqualified messages are sent
Fire up your irb to learn a little bit more about that
536/37 > irb
irb(main):001:0> self
=> main
This is the default self provided by the Ruby Interpreter
irb(main):002:0> class Test
look what happens to self when inside a class statement
irb(main):003:1> puts self
irb(main):004:1> end
Test
=> nil
irb(main):005:0> class Test
say_hello is sent to self, which is Test in this context
irb(main):006:1> say_hello
but Test does not reply to this message yet
irb(main):007:1> end
NameError: undefined local variable or method `say_hello’ for Test:Class
from (irb):6
from :0
irb(main):008:0> class Test
The def statement defines an instance method on self, we have
therefore
to write Test.say_hello in order to tell def a method on Test
itself, but you
seem to know that already
irb(main):009:1> def Test.say_hello
irb(main):010:2> puts “Hello”
irb(main):011:2> end
irb(main):012:1> end
=> nil
irb(main):013:0> class Test
self is set to Test again and now Test responds to the #say_hello
message
irb(main):014:1> say_hello
irb(main):015:1> end
Hello
=> nil
and just to show that the main object was not influenced by what
we defined for Test
irb(main):020:0> say_hello
NameError: undefined local variable or method `say_hello’ for
main:Object
from (irb):20
from :0
irb(main):021:0>
end
Normally,we can use a class anyplaces after defining it ,but in this
example,i use a classmehod immediately after defining it “in class
context”.This is valid.I means calling a classmethod(not an intance
method) in a class defintion–not after defining that class–is valid
.Can you explain it for me? thank you !
Funny but you explained it quite well, if there were no ? I would say
you have made a correct statement. Maybe you are just puzzled by the
dynamic nature of Ruby that everything springs into life when
executed.
class Test
dosomething
end
as a matter of fact is equivalent to
Test = Class::new
class Test
#(1)
doesomething
end
meaning that at point (1) Test is a well defined complete Ruby class
already
HTH
Robert