ruby pass like statement

R

rtilley

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
 
F

Farrel Lifson

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

rtilley

Farrel said:
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???
 
D

Daniel Harple

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
 
F

Farrel Lifson

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
 
J

Jeff Cohen

rtilley said:
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
 
C

Caleb Clausen

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.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: ruby pass like statement"

|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.
 
R

rtilley

Yukihiro said:
Hi,

In message "Re: ruby pass like statement"

|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
 
M

Marcin Mielżyński

rtilley said:
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
 
D

Doug H

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.
 
T

Troy Denkinger

------=_Part_3504_15607776.1141610026991
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

# TODO

Works fine.

Troy

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.

------=_Part_3504_15607776.1141610026991--
 
J

James Edward Gray II

# TODO

Works fine.

Then make an editor macro that collects them, HTMLifies them, and
allows you to just to any "link" to address the issue. Or by
Textmate which works this way out of the box. ;)

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top