Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how can i use a ruby class in object way

Reply
Thread Tools

how can i use a ruby class in object way

 
 
Guo Yangguang
Guest
Posts: n/a
 
      07-26-2007
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 !
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
FireAphis
Guest
Posts: n/a
 
      07-26-2007
On Jul 26, 6:43 am, Guo Yangguang <(E-Mail Removed)> wrote:
> 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 !
> --
> 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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      07-26-2007
2007/7/26, Guo Yangguang <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
Guo Yangguang
Guest
Posts: n/a
 
      07-29-2007
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 Yangguang <(E-Mail Removed)> 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/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      07-29-2007
On 7/26/07, Robert Klemme <(E-Mail Removed)> wrote:
<snip>
>

Small typo here
> ARGV.each do |line|

ARGF
> puts line
> end
>
> Kind regards
>
> robert
>
>


Robert
--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      07-29-2007
On 7/29/07, Guo Yangguang <(E-Mail Removed)> 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 <(E-Mail Removed)> 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

 
Reply With Quote
 
Guo Yangguang
Guest
Posts: n/a
 
      07-29-2007
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 Dober wrote:
> On 7/29/07, Guo Yangguang <(E-Mail Removed)> 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


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      07-29-2007
On 7/29/07, Guo Yangguang <(E-Mail Removed)> 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 http://www.ruby-doc.org/docs/ProgrammingRuby/

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
>
>
>
>
>
>
>
>
> Robert Dober wrote:
> > On 7/29/07, Guo Yangguang <(E-Mail Removed)> 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

>
> --
> Posted via http://www.ruby-forum.com/.
>
>



--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-29-2007
On 29.07.2007 09:30, Robert Dober wrote:
> On 7/26/07, Robert Klemme <(E-Mail Removed)> wrote:
> <snip>
> Small typo here
>> ARGV.each do |line|

> ARGF
>> puts line
>> end


Right. Thanks for catching the copy & paste error!

Kind regards

robert
 
Reply With Quote
 
Guo Yangguang
Guest
Posts: n/a
 
      07-29-2007
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.





--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
why can't derived class pointer can't point to base class object ypjofficial@indiatimes.com C++ 1 12-24-2004 08:46 AM
why can't derived class pointer can't point to base class object ypjofficial@indiatimes.com C++ 0 12-24-2004 06:34 AM



Advertisments