how can i use a ruby class in object way

Discussion in 'Ruby' started by Guo Yangguang, Jul 26, 2007.

  1. 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/.
    Guo Yangguang, Jul 26, 2007
    #1
    1. Advertising

  2. Guo Yangguang

    FireAphis Guest

    Re: how can i use a ruby class in object way

    On Jul 26, 6:43 am, Guo Yangguang <> 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
    FireAphis, Jul 26, 2007
    #2
    1. Advertising

  3. Re: how can i use a ruby class in object way

    2007/7/26, Guo Yangguang <>:
    > 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
    Robert Klemme, Jul 26, 2007
    #3
  4. Re: how can i use a ruby class in object way

    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 <> 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/.
    Guo Yangguang, Jul 29, 2007
    #4
  5. Guo Yangguang

    Robert Dober Guest

    Re: how can i use a ruby class in object way

    On 7/26/07, Robert Klemme <> 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
    Robert Dober, Jul 29, 2007
    #5
  6. Guo Yangguang

    Robert Dober Guest

    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
    Robert Dober, Jul 29, 2007
    #6
  7. Re: how can i use a ruby class in object way

    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 <> 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/.
    Guo Yangguang, Jul 29, 2007
    #7
  8. Guo Yangguang

    Robert Dober Guest

    Re: how can i use a ruby class in object way

    On 7/29/07, Guo Yangguang <> 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 <> 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
    Robert Dober, Jul 29, 2007
    #8
  9. Re: how can i use a ruby class in object way

    On 29.07.2007 09:30, Robert Dober wrote:
    > On 7/26/07, Robert Klemme <> wrote:
    > <snip>
    > Small typo here
    >> ARGV.each do |line|

    > ARGF
    >> puts line
    >> end


    Right. Thanks for catching the copy & paste error!

    Kind regards

    robert
    Robert Klemme, Jul 29, 2007
    #9
  10. Re: how can i use a ruby class in object way

    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/.
    Guo Yangguang, Jul 29, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,708
    Thomas Weidenfeller
    Oct 12, 2005
  2. Replies:
    1
    Views:
    1,282
    Jonathan Mcdougall
    Dec 24, 2004
  3. Replies:
    0
    Views:
    505
  4. Replies:
    7
    Views:
    997
    Daniel Pitts
    Sep 29, 2007
  5. Xeno Campanoli
    Replies:
    5
    Views:
    149
    Chris Hulan
    Apr 20, 2010
Loading...

Share This Page