• Using functions to return something

    From Cai Gengyang@21:1/5 to All on Tue Apr 19 07:06:59 2016
    --- So I managed to write a simple function and use return and puts to display it.

    def add(a, b)
    puts "ADDING #{a} + #{b}"
    return a + b
    end

    age = add(30, 5)

    puts "Age: #{age}"

    Display :

    CaiGengYangs-MacBook-Pro:GoatBoy CaiGengYang$ ruby ex80.rb
    ADDING 30 + 5
    Age: 35

    --- But when I tried to extend it to (A+B)/C, it display error message :

    def add(a, b)
    puts "(ADDING #{a} + #{b}) / #{c}"
    return (a + b) / c
    end

    age = add(30, 10) / 5

    puts "Age: #{age}"

    Display :

    CaiGengYangs-MacBook-Pro:GoatBoy CaiGengYang$ ruby ex90.rb
    ex90.rb:2:in `add': undefined local variable or method `c' for main:Object (NameError)
    from ex90.rb:6:in `<main>''

    Any smart person over here ? Help lah ...

    Gengyang

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Klemme@21:1/5 to Cai Gengyang on Tue Apr 19 19:19:19 2016
    On 19.04.2016 16:06, Cai Gengyang wrote:
    --- So I managed to write a simple function and use return and puts to display it.

    def add(a, b)
    puts "ADDING #{a} + #{b}"
    return a + b
    end

    age = add(30, 5)

    puts "Age: #{age}"

    Display :

    CaiGengYangs-MacBook-Pro:GoatBoy CaiGengYang$ ruby ex80.rb
    ADDING 30 + 5
    Age: 35

    --- But when I tried to extend it to (A+B)/C, it display error message :

    def add(a, b)
    puts "(ADDING #{a} + #{b}) / #{c}"
    return (a + b) / c
    end

    age = add(30, 10) / 5

    puts "Age: #{age}"

    Display :

    CaiGengYangs-MacBook-Pro:GoatBoy CaiGengYang$ ruby ex90.rb
    ex90.rb:2:in `add': undefined local variable or method `c' for main:Object (NameError)
    from ex90.rb:6:in `<main>''

    Any smart person over here ? Help lah ...

    The error message really says it all.

    robert


    --
    remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cai Gengyang@21:1/5 to Robert Klemme on Tue Apr 19 22:28:59 2016
    This is what I did :

    I created a file , defined a new variable c and saved it as ex50.rb ---

    def add(a, b, c)
    puts "(ADDING #{a} + #{b}) / #{c}"
    return (a + b) / c
    end

    age = add(30, 10) / 5

    puts "Age: #{age}"

    Then I tried to run it :

    CaiGengYangs-MacBook-Pro:~ CaiGengYang$ ruby ex50.rb

    But I got this error message :

    ruby: No such file or directory -- ex50.rb (LoadError)





    On Wednesday, April 20, 2016 at 1:19:26 AM UTC+8, Robert Klemme wrote:
    On 19.04.2016 16:06, Cai Gengyang wrote:
    --- So I managed to write a simple function and use return and puts to display it.

    def add(a, b)
    puts "ADDING #{a} + #{b}"
    return a + b
    end

    age = add(30, 5)

    puts "Age: #{age}"

    Display :

    CaiGengYangs-MacBook-Pro:GoatBoy CaiGengYang$ ruby ex80.rb
    ADDING 30 + 5
    Age: 35

    --- But when I tried to extend it to (A+B)/C, it display error message :

    def add(a, b)
    puts "(ADDING #{a} + #{b}) / #{c}"
    return (a + b) / c
    end

    age = add(30, 10) / 5

    puts "Age: #{age}"

    Display :

    CaiGengYangs-MacBook-Pro:GoatBoy CaiGengYang$ ruby ex90.rb
    ex90.rb:2:in `add': undefined local variable or method `c' for main:Object (NameError)
    from ex90.rb:6:in `<main>''

    Any smart person over here ? Help lah ...

    The error message really says it all.

    robert


    --
    remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sebastian Christ@21:1/5 to All on Wed Apr 20 09:25:10 2016
    What happened to "GoatBoy CaiGengYang"? It seems your other exercise
    live in that directory?

    Regards,
    Sebastian

    --
    Sebastian (Rudolfo) Christ
    http://rudolfochrist.github.io
    GPG Fingerprint: 306D 8FD3 DFB6 4E44 5061
    CE71 6407 D6F8 2AC5 55DD

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From jacki@21:1/5 to Cai Gengyang on Wed Apr 20 10:30:20 2016
    W dniu 2016-04-20 o 07:28, Cai Gengyang pisze:

    This is what I did :

    I created a file , defined a new variable c and saved it as ex50.rb ---

    def add(a, b, c)

    a, b and c - three arguments


    age = add(30, 10) / 5


    then you are calling function with two arguments and divide what
    function returns by 5 - really?

    And remember, that 7/3 = 2 not 2.333(3) because:
    2.2.0 :005 > 7.class
    Fixnum

    and Fixnum / Fixnum = Fixnum

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cai Gengyang@21:1/5 to Sebastian Christ on Wed Apr 20 04:12:52 2016
    Saved it in mystuff directory actually rather than Goatboy.

    Never mind ... gonna move on for the time being. (This was just a side issue I suddenly thought of). I wanna complete the entire ebook, then start looking at some open-source code and start making a web-based game.

    Are there more Ruby game engines online ?
    This is the only one I could find : http://blowmage.com/2015/04/17/writing-games-ruby


    On Wednesday, April 20, 2016 at 3:25:17 PM UTC+8, Sebastian Christ wrote:
    What happened to "GoatBoy CaiGengYang"? It seems your other exercise
    live in that directory?

    Regards,
    Sebastian

    --
    Sebastian (Rudolfo) Christ
    http://rudolfochrist.github.io
    GPG Fingerprint: 306D 8FD3 DFB6 4E44 5061
    CE71 6407 D6F8 2AC5 55DD

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sebastian Christ@21:1/5 to Cai Gengyang on Wed Apr 20 15:42:39 2016
    If you know that you've saved your file in "mystuff", how comes that
    you're puzzled by this?

    On 2016-04-19 22:28, Cai Gengyang <gengyangcai@gmail.com> wrote:
    Then I tried to run it :

    CaiGengYangs-MacBook-Pro:~ CaiGengYang$ ruby ex50.rb

    But I got this error message :

    ruby: No such file or directory -- ex50.rb (LoadError)

    --
    Sebastian (Rudolfo) Christ
    http://rudolfochrist.github.io
    GPG Fingerprint: 306D 8FD3 DFB6 4E44 5061
    CE71 6407 D6F8 2AC5 55DD

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Sebastian Christ on Wed Apr 20 14:34:38 2016
    On 2016-04-20, Sebastian Christ <rudolfo.christ@gmail.com> wrote:
    If you know that you've saved your file in "mystuff", how comes that
    you're puzzled by this?

    #15 ("WACF") in "Academic Programmers: A Spotter's Guide".

    http://www.ee.ryerson.ca/~elf/hack/academic.html

    (Not implying that the slobbering imbecile is an academic in any way).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Sebastian Christ on Wed Apr 20 19:27:15 2016
    On 2016-04-20, Sebastian Christ <rudolfo.christ@gmail.com> wrote:
    On 2016-04-20 14:34, Kaz Kylheku <545-066-4921@kylheku.com> wrote:

    On 2016-04-20, Sebastian Christ <rudolfo.christ@gmail.com> wrote:
    If you know that you've saved your file in "mystuff", how comes that
    you're puzzled by this?

    #15 ("WACF") in "Academic Programmers: A Spotter's Guide".

    http://www.ee.ryerson.ca/~elf/hack/academic.html

    (Not implying that the slobbering imbecile is an academic in any
    way).

    LOL. Very interesting read ;-) Thanks for the link.

    The author was a visionary when writing #19 "What Colour Should That
    Be?" Look at the state of the web today. And:

    "Always has a more expensive machine than you, usually with a very nice
    colour screen, sound card, Dataglove, voice recognition equipment etc. -
    and no keyboard ..."

    No keyboard! Hello, smartphone, tablet.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sebastian Christ@21:1/5 to Kaz Kylheku on Wed Apr 20 20:48:21 2016
    On 2016-04-20 14:34, Kaz Kylheku <545-066-4921@kylheku.com> wrote:

    On 2016-04-20, Sebastian Christ <rudolfo.christ@gmail.com> wrote:
    If you know that you've saved your file in "mystuff", how comes that
    you're puzzled by this?

    #15 ("WACF") in "Academic Programmers: A Spotter's Guide".

    http://www.ee.ryerson.ca/~elf/hack/academic.html

    (Not implying that the slobbering imbecile is an academic in any
    way).

    LOL. Very interesting read ;-) Thanks for the link.

    -Sebastian

    --
    Sebastian (Rudolfo) Christ http://rudolfochrist.github.io GPG
    Fingerprint: 306D 8FD3 DFB6 4E44 5061
    CE71 6407 D6F8 2AC5 55DD

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)