Returning?

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 11/15/05, Onur T. [email protected] 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.
:slight_smile:

Hi !

2005/11/15, Joe Van D. [email protected]:

On 11/15/05, Onur T. [email protected] 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 !

you can see the code in typo helpers :slight_smile: it’s valid. be sure :slight_smile:

does anyone understand what does it write in K page :slight_smile: I have no clue

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

Ruby threads aren’t real threads yet - blocking inside a thread can
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 :wink:

On 11/15/05, Phil S. [email protected] 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.

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

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 11/15/05, Phil S. [email protected] 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.

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

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.

Onur T. wrote:

in what way it is different than?

code = []
code << “something”
code << “another thing”

Yes. It is much slower :wink: And with YARV, even more so :frowning: 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.

thanks in advance

welcome.

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
did add app/services to ADDITIONAL_LOAD_PATH in environment.rb).

A simmilar pattern could be used for your application. Every minute a
cron job calls into your rails application and updates things as
required.

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


Cheers,

Peter D.
RealityForge.org: http://www.realityforge.org/