Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7235

Re: Using functions to return something

X-Received by 10.50.33.71 with SMTP id p7mr4827103igi.10.1461130139923; Tue, 19 Apr 2016 22:28:59 -0700 (PDT)
X-Received by 10.182.153.231 with SMTP id vj7mr76537obb.17.1461130139897; Tue, 19 Apr 2016 22:28:59 -0700 (PDT)
Path csiph.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!g8no4647721igr.0!news-out.google.com!u9ni175igk.0!nntp.google.com!g8no4647714igr.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.ruby
Date Tue, 19 Apr 2016 22:28:59 -0700 (PDT)
In-Reply-To <dnn7koFrj3gU1@mid.individual.net>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=2404:e800:ee12:41:5144:79eb:a117:888e; posting-account=V6fVrwoAAAB_KNNhh7gdmvqhYlmrIkSO
NNTP-Posting-Host 2404:e800:ee12:41:5144:79eb:a117:888e
References <b1e27c74-cf34-49c3-a4a1-85f55f0e55cd@googlegroups.com> <dnn7koFrj3gU1@mid.individual.net>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <69f480e7-0d0b-4fc7-bd69-49fc2f6540c4@googlegroups.com> (permalink)
Subject Re: Using functions to return something
From Cai Gengyang <gengyangcai@gmail.com>
Injection-Date Wed, 20 Apr 2016 05:28:59 +0000
Content-Type text/plain; charset=ISO-8859-1
Lines 72
Xref csiph.com comp.lang.ruby:7235

Show key headers only | View raw


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/

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Using functions to return something Cai Gengyang <gengyangcai@gmail.com> - 2016-04-19 07:06 -0700
  Re: Using functions to return something Robert Klemme <shortcutter@googlemail.com> - 2016-04-19 19:19 +0200
    Re: Using functions to return something Cai Gengyang <gengyangcai@gmail.com> - 2016-04-19 22:28 -0700
      Re: Using functions to return something Sebastian Christ <rudolfo.christ@gmail.com> - 2016-04-20 09:25 +0200
        Re: Using functions to return something Cai Gengyang <gengyangcai@gmail.com> - 2016-04-20 04:12 -0700
          Re: Using functions to return something Sebastian Christ <rudolfo.christ@gmail.com> - 2016-04-20 15:42 +0200
            Re: Using functions to return something Kaz Kylheku <545-066-4921@kylheku.com> - 2016-04-20 14:34 +0000
              Re: Using functions to return something Sebastian Christ <rudolfo.christ@gmail.com> - 2016-04-20 20:48 +0200
                Re: Using functions to return something Kaz Kylheku <545-066-4921@kylheku.com> - 2016-04-20 19:27 +0000
      Re: Using functions to return something jacki <collector@idealan.pl> - 2016-04-20 10:30 +0200

csiph-web