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.
043efdc2a79afbfec84696f50fd42163?d=identicon&s=25 onurturgay (Guest)
on 2005-11-15 21:07
(Received via mailing list)
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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 joevandyk (Guest)
on 2005-11-15 21:13
(Received via mailing list)
On 11/15/05, Onur Turgay <onurturgay@gmail.com> 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.
:-)
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 francois.beausoleil (Guest)
on 2005-11-15 21:31
(Received via mailing list)
Hi !

2005/11/15, Joe Van Dyk <joevandyk@gmail.com>:
> On 11/15/05, Onur Turgay <onurturgay@gmail.com> 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 !
043efdc2a79afbfec84696f50fd42163?d=identicon&s=25 onurturgay (Guest)
on 2005-11-15 21:46
(Received via mailing list)
you can see the code in typo helpers :) it's valid. be sure :)
043efdc2a79afbfec84696f50fd42163?d=identicon&s=25 onurturgay (Guest)
on 2005-11-15 21:52
(Received via mailing list)
does anyone understand what does it write in `K` page :) I have no clue
Fba04dfe5f677c48caee082d87c15b9f?d=identicon&s=25 phil (Guest)
on 2005-11-15 21:58
(Received via mailing list)
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
B1102f65359ee629df508c7857f03b1c?d=identicon&s=25 sitharus (Guest)
on 2005-11-15 22:01
(Received via mailing list)
On 16/11/2005, at 9:57 AM, Phil Swenson 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.
Fba04dfe5f677c48caee082d87c15b9f?d=identicon&s=25 phil (Guest)
on 2005-11-15 22:29
(Received via mailing list)
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.
B1102f65359ee629df508c7857f03b1c?d=identicon&s=25 sitharus (Guest)
on 2005-11-15 22:38
(Received via mailing list)
On 16/11/2005, at 10:27 AM, Phil Swenson 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 ;)
A777f1a2049d78a12ead38efb8f75f97?d=identicon&s=25 tanner.burson (Guest)
on 2005-11-15 23:02
(Received via mailing list)
On 11/15/05, Phil Swenson <phil@collectiveintellect.com> 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.
57e939ff00f9df2670a09f417c08da5e?d=identicon&s=25 joshknowles (Guest)
on 2005-11-15 23:05
(Received via mailing list)
On 11/15/05, Phil Swenson <phil@collectiveintellect.com> 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.
Dfc2a545a0c4f542d69f89a782714e8e?d=identicon&s=25 jqshenker (Guest)
on 2005-11-15 23:08
(Received via mailing list)
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
67b6389be42524fbd776e44fd35c3d7e?d=identicon&s=25 peter.j.donald (Guest)
on 2005-11-15 23:17
(Received via mailing list)
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...

--
Cheers,

Peter Donald
RealityForge.org: http://www.realityforge.org/
Fba04dfe5f677c48caee082d87c15b9f?d=identicon&s=25 phil (Guest)
on 2005-11-16 01:08
(Received via mailing list)
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
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 skaes (Guest)
on 2005-11-16 07:37
(Received via mailing list)
Onur Turgay 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.

>thanks in advance
>
>
welcome.
This topic is locked and can not be replied to.