External Ruby Script in HTML

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:

How would I make the function call?

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 :

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

<%= greeting %>

...

I hope this will help.

Naked S. a écrit :

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.

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

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/classes/ERB.html 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 S. 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/clientserver
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.

Naked S. [email protected] writes:

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

The answer is “no”. :slight_smile:

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

From: [email protected]:

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:

How would I make the function call?

if you mean like javascript, there is something close, and it’s
activescriptruby
http://www.geocities.co.jp/SiliconValley-PaloAlto/9251/ruby/main.html

close since it runs only on windows ie.

hope someone would write a ruby equivalent to javascript. that would be
a killer.

kind regards -botp

On 12/22/06, Peña, Botp [email protected] wrote:

hope someone would write a ruby equivalent to javascript. that would be a killer.

javascript is a rubyer language than it looks:

http://whytheluckystiff.net/clog/ruby/rubyDotJs.html

martin

For someone that likes Ruby, Javascript does not matter.
It feels like a crippled down, specialized language.

Ruby syntax would be a lot nicer.

And that Javascript is standardized doesnt make it
better. Its ok if everyone can use standardized
Javascript - but I personally, I’d dump Javascript
the moment it would be possible to live with
pure Ruby instead. But guess Javascript is stuck
for years :wink:

Peña wrote:

hope someone would write a ruby equivalent to javascript. that would be a killer.

It’s rather unprobable this will happen. Firstly, JavaScript is
standardised, for better or worse. I’ve seen the first bout of browser
wars, I’ve seen the second, Lite edition (e.g. how much of Dojo will
break on $BROWSER) - yay, very subtly incompatible handling of
window.location.href updates and onload/onclose events; I don’t want to
see the third bout, aka how many scripting language interpreters will
you bundle with or half-assedly reimplement in $BROWSER.

Not to mention the development principles of the two languages (or
rather standard libraries) are almost contrary, you’d have to rip out
and retrofit significant amounts of Ruby to be able to embed it safely.

David V.

<…>

(So, can
anyone recall actual use of XHTML 1.1 in the wild?).
<…>

Yeeep: Musings

Regards,
Rimantas

On 12/23/06, David V. [email protected] wrote:

By the way, you’re confusing language and programming environment. JS
the language is, in terms of language power, incrementally or at worst
linearly inferior than Ruby the language, and could probably match it in
more features than you can think of. The standard library is Good Enough
for the task the language is primarily supposed to fulfill (script a
HTML rendering engine).

Anyone thinking of Javascript as crippled or inferior would do well to
take a look at Loell

martin

Marc H. wrote:

For someone that likes Ruby, Javascript does not matter.
It feels like a crippled down, specialized language.

It’s supposed to be specialised. Different roles and all that. Where
JS is used, you’re not supposed to be expressing complex logic anyway.
Before anyone mentions Ajax, refer to my gruesome bias against the
hack^Wconcept. It’s also supposed to be crippled. JS is a language for
a domain that requires restrictions first because of the possible impact
and woes fixing if there was a security issue with the language itself.

If I was a nastier person I’d say you’re missing the point by leagues.
Oh, wait, I -am-, so, you are. Ruby wasn’t around when the need for
scripting web browsers emerged. So, JS got into the standard. Standards
are the only thing that preserve the web from collapsing on itself in a
rain of tears and any touching them will lead to at best pain. (So, can
anyone recall actual use of XHTML 1.1 in the wild?). And even if Ruby
were around by then, it wouldn’t be the Ruby you know, the domain itself
would necessitate crippling and specialisation. Allowing it to be used
“pure” is completely impossible.

By the way, you’re confusing language and programming environment. JS
the language is, in terms of language power, incrementally or at worst
linearly inferior than Ruby the language, and could probably match it in
more features than you can think of. The standard library is Good Enough
for the task the language is primarily supposed to fulfill (script a
HTML rendering engine).

David V.

On 12/24/06, Luciano R. [email protected] wrote:

On 12/23/06, Martin DeMello [email protected] wrote:

Anyone thinking of Javascript as crippled or inferior would do well to
take a look at Loell

Very interesting, Martin. Thanks for the link.

But I must say I failed to notice how Loell would change the mind of
somebody who considers Javascript crippled or inferior (except to
demonstrate that Javascript is Turing Complete).

The guy obviously knows what he’s doing, and he had a choice of
languages in which to implement his stuff - he clearly felt javascript
was up to it. I would never have tried something like that in java or
c, for instance.

martin

On 12/23/06, Martin DeMello [email protected] wrote:

Anyone thinking of Javascript as crippled or inferior would do well to
take a look at Loell

Very interesting, Martin. Thanks for the link.

But I must say I failed to notice how Loell would change the mind of
somebody who considers Javascript crippled or inferior (except to
demonstrate that Javascript is Turing Complete).

Cheers, and a healthy, peaceful and productive 2007 to all!

Naked S. 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/book/web.html
http://nubyonrails.com/articles/2006/02/01/rjs-and-content-type-header

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 :stuck_out_tongue:

Incidentally, if you set up Ruby to output HTML in the above fashion,
the example call you used:

would have to be done as follows:

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

Fahim

Naked S. wrote:

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

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 :slight_smile: