# Forum: Ruby on Rails returning?

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.
on 2005-11-15 22:07
hi everyone,

what does returning do? it is used as

returning code =[] {
code << "something"
code << "another thing"
}

in what way it is different than?

code = []
code << "something"
code << "another thing"

thanks in advance
on 2005-11-15 22:13
On 11/15/05, Onur T. <removed_email_address@domain.invalid> wrote:
> hi everyone,
>
> what does returning do? it is used as
>
> returning code =[] {
>   code << "something"
>   code << "another thing"
> }

That's not valid Ruby.  I have no idea what that is supposed to do.

> in what way it is different than?
>
> code = []
> code << "something"
> code << "another thing"

The previous code section is not valid.  This code section is valid.
:-)
on 2005-11-15 22:31
Hi !

> On 11/15/05, Onur T. <removed_email_address@domain.invalid> wrote:
> > returning code =[] {
> >   code << "something"
> >   code << "another thing"
> > }
>
> That's not valid Ruby.  I have no idea what that is supposed to do.

Hmmm, shouldn't that evaluate to something like this:

returning(
code=[] do
code << 'something'
code << 'another thing'
end
)

Although I agree that the first syntax doesn't properly evalutate in
irb...

Anyway, see ActiveSupport:
# A Ruby-ized realization of the K combinator, courtesy of Mikael
Brockman.

I did find http://wiki.tcl.tk/1923 which talks about it.

Bye !
on 2005-11-15 22:46
you can see the code in typo helpers :) it's valid. be sure :)
on 2005-11-15 22:52
does anyone understand what does it write in K page :) I have no clue
on 2005-11-15 22:58
I need functionality for the equivalent of a servlet in Java.

Specifically, I'm writing a monitoring application.  This monitoring
applicaiton will be a web service hosted in rails, as well as a UI. but
I
also need to run a timer in a "servlet" that triggers polling various
resources to make sure they are running properly.  This servlet needs to
be
loaded and started when the web server (webrick) is loaded and the timer
will be started when the servlet is loaded.

Could someone point me the right direction?

Thanks,

phil
on 2005-11-15 23:01
On 16/11/2005, at 9:57 AM, Phil S. wrote:

> Could someone point me the right direction?
I would suggest building the monitoring app outside of Rails and
using drb or something similar to communicate. Keeping it inside the
application could cause problems, especially if the monitoring was
blocking on something. Starting at the same time isn't hard though,
just spawn a daemon process.
on 2005-11-15 23:29
I'd rather keep things simple for now... I need rails because I do need
to
host web services to collect data and I need a UI... I would need to
have 2
separate apps if I went with 2 processes.

You said "just spawn a daemon process"... well, two things.  1) where do
I
start the thread?  I can't just put it in a controller, I need it to
start
when the web server is initialized (like configuring for load servlet in
Tomcat for Java).  2) Do you mean fork the process?  I'm running windows
for
dev, as I recall forking doesn't work on windows?

Is there any equiv to the Java "timer" in ruby?

I guess I could just do an infinite loop in a separate thread and sleep
after every poll.
on 2005-11-15 23:38
On 16/11/2005, at 10:27 AM, Phil S. wrote:

> I'd rather keep things simple for now... I need rails because I do
> need to
> host web services to collect data and I need a UI... I would need
> to have 2
> separate apps if I went with 2 processes.

Do one for web service collection, another for anything that might
block. You can still include various parts of Rails to help. Read
below for why.

> You said "just spawn a daemon process"... well, two things.  1)
> where do I
> start the thread?  I can't just put it in a controller, I need it
> to start
> when the web server is initialized (like configuring for load
> servlet in
> Tomcat for Java).  2) Do you mean fork the process?  I'm running
> windows for
> dev, as I recall forking doesn't work on windows?

block  your entire application. Anyway, put it in environment.rb,
that runs before your app loads. And no, Ruby can't fork on Windows
AFAIK, though if you install the NT UNIX layer the syscall should be
available, just not from Ruby...

> Is there any equiv to the Java "timer" in ruby?

Have a browse of http://www.ruby-doc.org/, but I don't recall anything.

> I guess I could just do an infinite loop in a separate thread and
> sleep
> after every poll.
It works ;)
on 2005-11-16 00:02
On 11/15/05, Phil S. <removed_email_address@domain.invalid> wrote:
>
> I'd rather keep things simple for now... I need rails because I do need to
> host web services to collect data and I need a UI... I would need to have
> 2
> separate apps if I went with 2 processes.

Yes you would.

You said "just spawn a daemon process"... well, two things. 1) where do
I
> start the thread? I can't just put it in a controller, I need it to start
> when the web server is initialized (like configuring for load servlet in
> Tomcat for Java).

Because of the 'shared nothing' mentality of a rails app, it's a bad
idea to
launch this from within the web app. You will end up blocking a
(lighttpd |
apache | webrick) process indefinitely.

2) Do you mean fork the process? I'm running windows for
> dev, as I recall forking doesn't work on windows?

In *nix yes, you could check for the existance of a process and if it
isn't
there, then fork. I think via a win32utils package you can do the
equivalant
of a fork from windows.

Is there any equiv to the Java "timer" in ruby?

Ruby != Rails
So yes, you could easily do this in Ruby, via the use of DRb or
something
similar. Then in a controller in your Rails app you could poll your DRb
server (for all intents this could be a little Java TCP server as well)
and
return the results.

I guess I could just do an infinite loop in a separate thread and sleep
> after every poll.

See above answer about blocking web server processes.
on 2005-11-16 00:05
On 11/15/05, Phil S. <removed_email_address@domain.invalid> wrote:

> Is there any equiv to the Java "timer" in ruby?
>
> I guess I could just do an infinite loop in a separate thread and sleep
> after every poll.

Why not run the script as a cron job and access your rails environment
using
script/running. This way every hour your cron will signal to the rails
app
that its time to wake up.
on 2005-11-16 00:08
I would highly recommend a separate Ruby script that uses DRb to
communicate with the webapp. DRb is drop-dead simple, and it's also
flexible: you can run the webapp and script on different hosts if you
so choose, for example. You can even have this script load your Rails
environment (see the Rails wiki for more) if you want to connect to
the db independently or use your Rails models inside the script. I'm
using this method, and it works very well.

Jacob
on 2005-11-16 00:17
Hi,

The way I would suggest you implement this is to have cron or
equivelent periodically invoke methods using script/runner. For a
simple opensource example I would suggest you have a look at CIA
continuous integration project[1]. It will call "into" the rails
application everytime a change occurs in subversion using something
like;

export REPOS=...
export REV=...
script/runner "Agent.build(\"$REPOS\",$REV)"

Agent.build is a class method in app/services/agent.rb (Note that they

A simmilar pattern could be used for your application. Every minute a
required.

[1]
http://wiki.rubyonrails.com/rails/pages/How+To+Use...

--
Cheers,

Peter D.
RealityForge.org: http://www.realityforge.org/
on 2005-11-16 02:08
Thanks for all the comments... I'll go with the suggested architecture
(shared nothing)...

Here's a couple links an explanation of shared nothing:
http://www.loudthinking.com/arc/000479.html

http://db.cs.berkeley.edu/papers/hpts85-nothing.pdf
on 2005-11-16 08:37
Onur T. wrote:

>in what way it is different than?
>
>code = []
>code << "something"
>code << "another thing"
>
>
>
>
Yes. It is much slower ;-) And with YARV, even more so :-( I suggest to
just avoid it.

"returning" is derived from the K combinator of lambda calculus, where
it is defined as K == \lambda x. \lambda y. x.

In other words: K x y == x. The ruby version added to ActiveSupport is
defined as

def returning(value); yield; value; end

So "returning exp block" evaluates exp, invokes block for side effects
and then returns the value of exp.

The "correct" definition of returning would have been

def returning(value); yield(value); value; end

But the code example above would then have to be written as:

returning [] do |code|
code << "something"
code << "another thing"
end

Which shows that its application in this context is really a bit silly,
because

[] << "something" << "another thing"

or even

[ "something", "another thing" ]

does the same thing and is much more efficient for Ruby to execute. It's
really only useful if the last statement does not return exp's value,
like in this example:

returning code=[] do
code << "something"
code << "another thing" if condition
end

But adding code as last statement fixes this easily.

Just my 2c.

welcome.