[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby pass like statement

Brad Tilley

3/5/2006 5:18:00 PM

When laying out programs in Python, sometimes during preliminary design,
functions/methods are named but not defined. Instead, 'pass' is used to
indicate that it'll be defined later. It may look like this:

def some_function():
# This function will...
pass

Does Ruby have something similar?

Thanks,
Brad
12 Answers

Farrel Lifson

3/5/2006 5:23:00 PM

0

Does pass actually do anything like print to stdout "some_function not
yet defined"?


On 3/5/06, rtilley <rtilley@vt.edu> wrote:
> When laying out programs in Python, sometimes during preliminary design,
> functions/methods are named but not defined. Instead, 'pass' is used to
> indicate that it'll be defined later. It may look like this:
>
> def some_function():
> # This function will...
> pass
>
> Does Ruby have something similar?
>
> Thanks,
> Brad
>
>


Brad Tilley

3/5/2006 5:30:00 PM

0

Farrel Lifson wrote:
> Does pass actually do anything like print to stdout "some_function not
> yet defined"?

No, it's just a place holder.

It would be incorrect (in Python) to not define the function. So, you
place the 'pass' statement there to avoid causing errors when testing
your program. Without the pass statement, I believe one would be
required to comment out the undefined function in order to execute the
program. Maybe this is not needed in Ruby???

Daniel Harple

3/5/2006 5:32:00 PM

0


On Mar 5, 2006, at 6:18 PM, rtilley wrote:

> When laying out programs in Python, sometimes during preliminary
> design, functions/methods are named but not defined. Instead,
> 'pass' is used to indicate that it'll be defined later. It may look
> like this:
>
> def some_function():
> # This function will...
> pass
>
> Does Ruby have something similar?
>
> Thanks,
> Brad
>

def some_funtion
end


Farrel Lifson

3/5/2006 5:34:00 PM

0

If the method is not defined the interpreter will throw a
NoMethodError exception.

If you don't want that you can just define your method with an empty body

class Test
def foo
end
end

This will return a nil if called.

Farrel


On 3/5/06, rtilley <rtilley@vt.edu> wrote:
> Farrel Lifson wrote:
> > Does pass actually do anything like print to stdout "some_function not
> > yet defined"?
>
> No, it's just a place holder.
>
> It would be incorrect (in Python) to not define the function. So, you
> place the 'pass' statement there to avoid causing errors when testing
> your program. Without the pass statement, I believe one would be
> required to comment out the undefined function in order to execute the
> program. Maybe this is not needed in Ruby???
>
>


Jeff

3/5/2006 5:35:00 PM

0

rtilley wrote:
> Farrel Lifson wrote:
>> Does pass actually do anything like print to stdout "some_function not
>> yet defined"?
>
> No, it's just a place holder.
>

Empty methods are acceptable in Ruby, and they have a return value of
nil.

def some_method
end

This way your code can call this method. It just won't do anything
useful until you go back and implement the method body later.

Jeff
www.softiesonrails.com

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


Caleb Clausen

3/5/2006 5:36:00 PM

0

On 3/5/06, rtilley <rtilley@vt.edu> wrote:
> When laying out programs in Python, sometimes during preliminary design,
> functions/methods are named but not defined. Instead, 'pass' is used to
> indicate that it'll be defined later. It may look like this:
>
> def some_function():
> # This function will...
> pass
>
> Does Ruby have something similar?

There's nothing like this that I know of, but you could always just pretend:

def some_function
pass
end

.... #some time later

def some_function
the_real_implementation
end

I think this will earn you a warning about some_function being defined
twice, so it's slightly ugly. The second def will overwrite the first,
so it should have the semantics you want... provided that second def
really does appear after the first. I don't know what the semantics of
pass in python are, but in ruby if you actually call (a method that
uses) pass, you'll get a NoMethodError at runtime. Unless you make a
method called 'pass', in which case you'll be in trouble.....

Alternatively, you could just use 'raise' or 'fail' instead, neither
of which should be defined in well-behaved ruby code.


Yukihiro Matsumoto

3/5/2006 5:39:00 PM

0

Hi,

In message "Re: ruby pass like statement"
on Mon, 6 Mar 2006 02:28:39 +0900, rtilley <rtilley@vt.edu> writes:

|Maybe this is not needed in Ruby???

No, when you want something empty, you write nothing in there in
Ruby, since it's empty.

def some_function():
end

No need for any placeholder like "pass" for nothing.

matz.
p.s.
I know, I know. We have to write "end" everywhere instead.


Brad Tilley

3/5/2006 7:08:00 PM

0

Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: ruby pass like statement"
> on Mon, 6 Mar 2006 02:28:39 +0900, rtilley <rtilley@vt.edu> writes:
>
> |Maybe this is not needed in Ruby???
>
> No, when you want something empty, you write nothing in there in
> Ruby, since it's empty.
>
> def some_function():
> end
>
> No need for any placeholder like "pass" for nothing.

Thank you all for the info.

Learning Ruby has been fun so far. With the Pick Axe book and this
forum, I've been able to pick-up a lot in one weekend.

Brad

Marcin Mielzynski

3/5/2006 9:43:00 PM

0

rtilley wrote:
> When laying out programs in Python, sometimes during preliminary design,
> functions/methods are named but not defined. Instead, 'pass' is used to
> indicate that it'll be defined later. It may look like this:
>
> def some_function():
> # This function will...
> pass
>
> Does Ruby have something similar?
>
> Thanks,
> Brad

pass is not needed in Ruby. Indentation forces python to have such a
statement since there would be no way to create empty class/method

empty function:

def fun():
pass

print fun()

will return None just like an empty method in Ruby returns nil.


lopex

Doug H

3/6/2006 1:42:00 AM

0

One thing I saw for perl6 that I liked is the 'yadda yadda' operator, 3
periods in a row: ...

def some_function()
...
end

It is sort of like a 'raise notimplementedexception'. A visual cue to
come back to this method later to implement it.