ruby script

Discussion in 'Ruby' started by Mlle, Mar 6, 2011.

  1. Mlle

    Mlle Guest

    Hi,

    I wrote a ruby script but I'd like to define a method within it and
    call the method from the main method. How do I do that? Do I have to
    define the whole file as a module or something?
    Thanks
     
    Mlle, Mar 6, 2011
    #1
    1. Advertising

  2. Mlle wrote in post #985808:
    > I wrote a ruby script but I'd like to define a method within it and
    > call the method from the main method. How do I do that? Do I have to
    > define the whole file as a module or something?


    Nope (although using modules can make your code easier to reuse and/or
    combine with other code)

    Simple example which I think achieves what you want:

    ---- a.rb ----
    def say_hello
    puts "Hello, world!"
    end

    ---- b.rb ----
    require 'a'
    say_hello

    ---- command line ----
    ruby b.rb

    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Mar 6, 2011
    #2
    1. Advertising

  3. Mlle

    Quintus Guest

    Am 06.03.2011 21:45, schrieb Mlle:
    > Hi,
    >
    > I wrote a ruby script but I'd like to define a method within it and
    > call the method from the main method. How do I do that? Do I have to
    > define the whole file as a module or something?
    > Thanks
    >
    >


    Ruby doesn't have a main method. Your script starts being executed at
    the very top and ends at the very bottom. Everything in between is
    executed line by line.

    How about:

    ====================
    def mymethod
    puts "Hello from my method!"
    end

    mymethod
    ====================

    ?

    Vale,
    Marvin
     
    Quintus, Mar 6, 2011
    #3
  4. Mlle

    Mlle Guest

    On Mar 6, 4:43 pm, Brian Candler <> wrote:
    > Mlle wrote in post #985808:
    >
    > > I wrote a ruby script but I'd like to define a method within it and
    > > call the method from the main method.  How do I do that?  Do I haveto
    > > define the whole file as a module or something?

    >
    > Nope (although using modules can make your code easier to reuse and/or
    > combine with other code)
    >
    > Simple example which I think achieves what you want:
    >
    > ---- a.rb ----
    > def say_hello
    >   puts "Hello, world!"
    > end
    >
    > ---- b.rb ----
    > require 'a'
    > say_hello
    >
    > ---- command line ----
    > ruby b.rb
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.


    I realized I was having issues because I defined the method I wanted
    to use after the script...obviously it should be defined before. Then
    it runs just fine without having to be a module.
     
    Mlle, Mar 6, 2011
    #4
    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. Rajat
    Replies:
    3
    Views:
    746
    Jorgen Grahn
    Jan 8, 2010
  2. Sy
    Replies:
    0
    Views:
    207
  3. Mario Gr
    Replies:
    3
    Views:
    327
    Mario Gr
    Jul 4, 2009
  4. Saravanan Sundaramoorthy

    how to test a ruby script from another ruby script?

    Saravanan Sundaramoorthy, Jul 17, 2010, in forum: Ruby
    Replies:
    1
    Views:
    159
    Roger Pack
    Jul 19, 2010
  5. 3lionz Wexler
    Replies:
    2
    Views:
    763
    Suraj Kurapati
    Aug 24, 2010
Loading...

Share This Page