Function declarations? 
Author Message
 Function declarations?

I was wondering if it possible to obtain a reference to a function before it
has been
'def'ined?

eg:

do_something(my_func)

def my_func():
    blahblah...
end

Python does not allow this, I wondered if it's possible with Ruby (as it is
with Perl, C, Java etc) ?



Fri, 09 May 2003 03:00:00 GMT  
 Function declarations?

Quote:

> I was wondering if it possible to obtain a reference to a function before it
> has been
> 'def'ined?

Sort of...

The reference to a function is by name, not by function pointer, so
you can use the name before the function exists:

   fn = case ARGV[0]
        when "clear"
             :doClear
        when "reset"
             :doReset
        else
             :doError
        end

 However the function must be defined before you can call it:

#    self.method(fn).call    # this would be an error

    def doClear
      puts "Clear"
    end
    def doReset
      puts "Reset"
    end
    def doError
      puts "Error"
    end

    self.method(fn).call     # This is OK

Regards

Dave



Sat, 10 May 2003 04:48:34 GMT  
 Function declarations?

Quote:


> > I was wondering if it possible to obtain a reference to a function before it
> > has been
> > 'def'ined?

> Sort of...

> The reference to a function is by name, not by function pointer, so
> you can use the name before the function exists:

>    fn = case ARGV[0]
>         when "clear"
>              :doClear
>         when "reset"
>              :doReset
>         else
>              :doError
>         end

>  However the function must be defined before you can call it:

> #    self.method(fn).call    # this would be an error

>     def doClear
>       puts "Clear"
>     end
>     def doReset
>       puts "Reset"
>     end
>     def doError
>       puts "Error"
>     end

>     self.method(fn).call     # This is OK

> Regards

> Dave

One way to help yourself think top-down, if you want to:

-----------------------
  #!/usr/local/bin/ruby

  def main
    # Top level code goes here....
    print succ(99),"\n"
  end

  # Support code goes here: functions, classes,
  # whatever, arranged in any convenient order....
  def succ(x)
    x+1
  end

  main
-----------------------

  Mark



Sat, 10 May 2003 06:34:48 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Trouble using variables for function names in function declarations

2. Trouble using variables for function names in function declarations

3. Function Declaration and what to send

4. Function declaration and use

5. ASCIIZ limmits in Function declaration

6. Updated function declarations for SocketTools

7. Function Declaration by Template generation...

8. Clean function declaration

9. function declarations

10. VHDL function declaration (variable)

11. "Picky" Multiple Function Declaration Error

12. F90 function declarations

 

 
Powered by phpBB® Forum Software