[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

External Ruby Script in HTML

Naked Sushi

12/21/2006 9:02:00 PM

This probably has a simple answer, but I don't know it.

Let's say I have a ruby script called hello.rb

#!/usr/bin/ruby
def greeting ()
return "Hello world!"
end

I want to call that script from within an HTML document. How would I do
so? Within the HTML document, would I just treat it the same way I call
external javascript:

<script src="path/to/hello.rb" type="text/ruby"></script>

How would I make the function call?

--
Posted via http://www.ruby-....

7 Answers

user@domain.invalid

12/21/2006 9:11:00 PM

0

Hi,

I think the best way is to use eruby. You then just have to put your
script inside '<%' and '%>' (no output in the html) or '<%=' and '%>'
(with output). For instance :

....
<body>
<% def greeting %>
<% "Hello world!" %>
<% end %>

<%= greeting %>
</body>
....

I hope this will help.

Naked Sushi a écrit :
> This probably has a simple answer, but I don't know it.
>
> Let's say I have a ruby script called hello.rb
>
> #!/usr/bin/ruby
> def greeting ()
> return "Hello world!"
> end
>
> I want to call that script from within an HTML document. How would I do
> so? Within the HTML document, would I just treat it the same way I call
> external javascript:
>
> <script src="path/to/hello.rb" type="text/ruby"></script>
>
> How would I make the function call?
>

Eric Schwartz

12/21/2006 9:16:00 PM

0

Naked Sushi <runfaster@runawaysquirrels.com> writes:
> This probably has a simple answer, but I don't know it.

The answer is "no". :)

> Let's say I have a ruby script called hello.rb
>
> #!/usr/bin/ruby
> def greeting ()
> return "Hello world!"
> end
>
> I want to call that script from within an HTML document. How would I do
> so?

You can't.

> Within the HTML document, would I just treat it the same way I call
> external javascript:

It's not javascript, so no.

> How would I make the function call?

Use eruby as someone suggested, use server-parsed html (SHTML), use a
CGI instead of a plain HTMl file, probably something else I haven't
thought of. But you can't do it with just plain HTML.

-=Eric

ramalho@gmail.com

12/21/2006 9:24:00 PM

0

> I want to call that script from within an HTML document. How would I do
> so? Within the HTML document, would I just treat it the same way I call
> external javascript:

The call to a script embedded or linked to an HTML document would have
to be performed by some software reading the HTML. That software could
be on the client side (like a browser) or on the server side (like an
HTTP server plugin). JavaScript usually runs in the browser, and so
<script> tags are normaly ignored by the server.

To embed Ruby in HTML, the most popular choice seems to be ERB:

http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classe...

That woud be interpreted on the server-side, provided your HTTP server
is properly configured.

If you want to use Ruby to generate dynamic pages, you should look
into Ruby on Rails or Camping. Those provide not only a way to embed
Ruby in HTML, but some extra functionality to build dynamc sites.

Cheers,

Luciano

Naked Sushi

12/21/2006 9:26:00 PM

0

côme wrote:
> I think the best way is to use eruby.

I think that's the problem. I can make it work with eruby, but I would
rather just have it as an external script.

--
Posted via http://www.ruby-....

Gavin Kistner

12/22/2006 6:00:00 AM

0

Naked Sushi wrote:
> #!/usr/bin/ruby
> def greeting ()
> return "Hello world!"
> end
>
> I want to call that script from within an HTML document. How would I do
> so?

Read this (it's very short):
http://xkr.us/js/cl...
When you see "PHP" in there, think "My Ruby Script". For your question,
when you see "JS", you can think "HTML".

Hopefully that very brief story will help you understand how the
'magic' of the WWW works; where your Ruby code can run on the server,
and why you can't run server code from a user's web browser. The only
thing you can do is use HTML and/or JavaScript to make a new request
back to the web server, and get the results from it.

Charles Oliver Nutter

12/24/2006 10:58:00 AM

0

Naked Sushi wrote:
> This probably has a simple answer, but I don't know it.
>
> Let's say I have a ruby script called hello.rb
>
> #!/usr/bin/ruby
> def greeting ()
> return "Hello world!"
> end
>
> I want to call that script from within an HTML document. How would I do
> so? Within the HTML document, would I just treat it the same way I call
> external javascript:
>
> <script src="path/to/hello.rb" type="text/ruby"></script>
>
> How would I make the function call?
>

I have been interested in making this happen using an applet in JRuby.
By wiring in a call to the applet that loads the ruby file, it should be
possible to get the applet to run the script and return some result. The
interplay between the page and the applet would have to go over
"liveconnect" or something similar to work.

The applet part is basically taken care of, and has been used to create
an IRB GUI that runs in a browser. The LiveConnect/javascript stuff I
have only done very cursory research into, but it seems that it should
be possible.

If you're interested in this angle further, you can contact me or join
the jruby lists...or just talk about it here, though I'm not as good
about checking ruby-talk lately :)

--
Charles Oliver Nutter, JRuby Core Developer
Blogging on Ruby and Java @ headius.blogspot.com
Help spec out Ruby today! @ www.headius.com/rubyspec
headius@headius.com -- charles.nutter@sun.com

Fahim Farook

12/27/2006 5:45:00 AM

0

Naked Sushi wrote:
> This probably has a simple answer, but I don't know it.
> Let's say I have a ruby script called hello.rb
> #!/usr/bin/ruby
> def greeting ()
> return "Hello world!"
> end

I believe what you're looking for is explained in these documents:
http://www.rubycentral.com/boo...
http://nubyonrails.com/articles/2006/02/01/rjs-and-content-t...

The first method is the easy one and will do exactly what you want - at
least, I think so since I have a similar requirement. I need to be able
to run Ruby on one server but be able to call a script on the first
server from a different server's HTML content. I've done something
similar with Python and PHP and it worked fine. So I believe the first
link examples should work as well though I still haven't tried it. The
second document about RJS appears to provide a more complicated way to
do the same thing but again I'm not absolutely certain since I haven't
tried it out :p

Incidentally, if you set up Ruby to output HTML in the above fashion,
the example call you used:
> <script src="path/to/hello.rb" type="text/ruby"></script>
would have to be done as follows:
<script src="path/to/hello.rb"></script>

Hope this helps, or at least puts you on the right track :)

Fahim

--
Posted via http://www.ruby-....