Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
how can i use a ruby class in object way
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Guo Yangguang  
View profile  
 More options Jul 25 2007, 11:43 pm
Newsgroups: comp.lang.ruby
From: Guo Yangguang <gy...@163.com>
Date: Thu, 26 Jul 2007 12:43:16 +0900
Local: Wed, Jul 25 2007 11:43 pm
Subject: how can i use a ruby class in object way
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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
FireAphis  
View profile  
 More options Jul 26 2007, 2:05 am
Newsgroups: comp.lang.ruby
From: FireAphis <FireAp...@gmail.com>
Date: Thu, 26 Jul 2007 15:05:06 +0900
Local: Thurs, Jul 26 2007 2:05 am
Subject: Re: how can i use a ruby class in object way
On Jul 26, 6:43 am, Guo Yangguang <gy...@163.com> 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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options Jul 26 2007, 3:05 am
From: "Robert Klemme" <shortcut...@googlemail.com>
Date: Thu, 26 Jul 2007 16:05:17 +0900
Local: Thurs, Jul 26 2007 3:05 am
Subject: Re: how can i use a ruby class in object way
2007/7/26, Guo Yangguang <gy...@163.com>:

> 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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guo Yangguang  
View profile  
 More options Jul 29 2007, 2:56 am
Newsgroups: comp.lang.ruby
From: Guo Yangguang <gy...@163.com>
Date: Sun, 29 Jul 2007 15:56:07 +0900
Local: Sun, Jul 29 2007 2:56 am
Subject: 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 !

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 29 2007, 3:30 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Sun, 29 Jul 2007 16:30:48 +0900
Local: Sun, Jul 29 2007 3:30 am
Subject: Re: how can i use a ruby class in object way
On 7/26/07, Robert Klemme <shortcut...@googlemail.com> 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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 29 2007, 3:35 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Sun, 29 Jul 2007 16:35:24 +0900
Local: Sun, Jul 29 2007 3:35 am
Subject: Re: how can i use a ruby class in object way
On 7/29/07, Guo Yangguang <gy...@163.com> 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

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guo Yangguang  
View profile  
 More options Jul 29 2007, 4:20 am
Newsgroups: comp.lang.ruby
From: Guo Yangguang <gy...@163.com>
Date: Sun, 29 Jul 2007 17:20:07 +0900
Local: Sun, Jul 29 2007 4:20 am
Subject: 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 !

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 29 2007, 5:11 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Sun, 29 Jul 2007 18:11:48 +0900
Local: Sun, Jul 29 2007 5:11 am
Subject: Re: how can i use a ruby class in object way
On 7/29/07, Guo Yangguang <gy...@163.com> 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

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options Jul 29 2007, 6:05 am
Newsgroups: comp.lang.ruby
From: Robert Klemme <shortcut...@googlemail.com>
Date: Sun, 29 Jul 2007 19:05:02 +0900
Local: Sun, Jul 29 2007 6:05 am
Subject: 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 <shortcut...@googlemail.com> 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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Logan Capaldo  
View profile  
 More options Jul 29 2007, 7:25 am
From: "Logan Capaldo" <logancapa...@gmail.com>
Date: Sun, 29 Jul 2007 20:25:51 +0900
Local: Sun, Jul 29 2007 7:25 am
Subject: Re: how can i use a ruby class in object way

On 7/29/07, Guo Yangguang <gy...@163.com> 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guo Yangguang  
View profile  
 More options Jul 29 2007, 9:06 am
Newsgroups: comp.lang.ruby
From: Guo Yangguang <gy...@163.com>
Date: Sun, 29 Jul 2007 22:06:50 +0900
Local: Sun, Jul 29 2007 9:06 am
Subject: 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/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google