Forum: Ruby ruby pass like statement

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
rtilley (Guest)
on 2006-03-05 19:21
(Received via mailing list)
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
Farrel L. (Guest)
on 2006-03-05 19:24
(Received via mailing list)
Does pass actually do anything like print to stdout "some_function not
yet defined"?
rtilley (Guest)
on 2006-03-05 19:30
(Received via mailing list)
Farrel L. 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 H. (Guest)
on 2006-03-05 19:33
(Received via mailing list)
On Mar 5, 2006, at 6:18 PM, rtilley wrote:

>
> Thanks,
> Brad
>

def some_funtion
end
Farrel L. (Guest)
on 2006-03-05 19:34
(Received via mailing list)
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
Jeff C. (Guest)
on 2006-03-05 19:34
rtilley wrote:
> Farrel L. 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
Caleb C. (Guest)
on 2006-03-05 19:37
(Received via mailing list)
On 3/5/06, rtilley <removed_email_address@domain.invalid> 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 M. (Guest)
on 2006-03-05 19:40
(Received via mailing list)
Hi,

In message "Re: ruby pass like statement"
    on Mon, 6 Mar 2006 02:28:39 +0900, rtilley 
<removed_email_address@domain.invalid> 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.
rtilley (Guest)
on 2006-03-05 21:09
(Received via mailing list)
Yukihiro M. wrote:
>   def some_function():
>   end
>
> No need for any placeholder like "pass" for nothing.

Thank you all for the info.

Learning R. 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 MielżyÅ?ski (Guest)
on 2006-03-05 23:44
(Received via mailing list)
rtilley wrote:
> 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 (Guest)
on 2006-03-06 03:45
(Received via mailing list)
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.
Troy D. (Guest)
on 2006-03-06 03:54
(Received via mailing list)
# TODO

Works fine.

Troy
James G. (Guest)
on 2006-03-06 04:34
(Received via mailing list)
On Mar 5, 2006, at 7:54 PM, Troy D. wrote:

> # 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 G. II
This topic is locked and can not be replied to.