[newbie] converting javascript to ruby (nested methods)

i have a script written in javascript and want to translate it into ruby
(using JRuby) but i face with the error about nested method which used
ofently in javascript.

The script controls a “Calculator” which gui is written in xpml, an xml
wrapper of java-swing components see :

then i wonder how to convert this, for example :

function doIt() {
if ( type.equals(“operand”) ) {
doOperand();
}
else {
doValue();
}
}

into a ruby script.
each calculator’s button as two fields associated with it a type (might
be operand (ie +, =, -, /, *…) or value (0…9) and a value .

obviously i’ve tried something like :

def doIt(type, value)
if type == "operand"
doOperand(type, value)
else
doValue(type, value)
end
end

what is the ruby way for such nested methods ?

···


yt

i have a script written in javascript and want to translate it into
ruby
(using JRuby) but i face with the error about nested method which used
ofently in javascript.

nested method? That doesn’t sound familiar. Could you post the actual
error printout that you get? That should help tack it down.

obviously i’ve tried something like :

def doIt(type, value)
if type == “operand”
doOperand(type, value)
else
doValue(type, value)
end
end

what is the ruby way for such nested methods ?

Honestly, this code looks fine to me, assuming you definded
“doOperand()” and “doValue()” elsewhere in the script. Could you post a
little more code? :slight_smile:

–Mark

···

On Mar 20, 2004, at 1:19 AM, Yvon Thoraval wrote:

“Yvon Thoraval” yvon.thoravalNO-SPAM@free.fr schrieb im Newsbeitrag
news:1gaxy9g.jksiqedi2kw8N%yvon.thoravalNO-SPAM@free.fr…

i have a script written in javascript and want to translate it into ruby
(using JRuby) but i face with the error about nested method which used
ofently in javascript.

The script controls a “Calculator” which gui is written in xpml, an xml
wrapper of java-swing components see :
http://www.ultrid.com/index.php?module=Static_Docs&func=view&newlang=eng

then i wonder how to convert this, for example :

function doIt() {
if ( type.equals(“operand”) ) {
doOperand();
}
else {
doValue();
}
}

into a ruby script.
each calculator’s button as two fields associated with it a type (might
be operand (ie +, =, -, /, *…) or value (0…9) and a value .

obviously i’ve tried something like :

def doIt(type, value)
if type == “operand”
doOperand(type, value)
else
doValue(type, value)
end
end

A direct translaction would have been:

def doIt
if my_type == “operand”
doOperand
else
doValue
end
end

Note the usage of my_type because type is already defined for each Ruby
object. But read on.

what is the ruby way for such nested methods ?

First of all this doesn’t seem to be about nested methods (i.e. methods
defined in the body of other methods) but about invoking methods from other
methods but much more about dispatching according to certain criteria (the
object’s type in this case).

From what I see this looks like a typical case of method overloading, which
is one of the most important features in object oriented languages: Method
invocation is done depending on the type of instance at hand (this, btw, can
be done similarly in JavaScript):

class Operand
def doIt
# do operand stuff
end
end

class Value
def doIt
# do value stuff
end
end

Now you just do

something = Operand.new
something.doIt
something = Value.new
something.doIt

If Operator and Value have some behavior (i.e. methods) in common, you can
even use inheritance, i.e. stick all common methods into a base class and
have sub classes derive from it:

class Base
def some_common_method
# whatever
end
end

class Operand < Base
def doIt
# do operand stuff
end
end

class Value < Base
def doIt
# do value stuff
end
end

something = Operand.new
something.some_common_method
something.doIt
something = Value.new
something.some_common_method
something.doIt

Now, here’s how you can achieve something similar with JavaScript (no
inheritance in this example). Paste this into a .html file and see what
happens:

Test

nested method? That doesn’t sound familiar. Could you post the actual
error printout that you get? That should help tack it down.

the error :

: [42, 0] nested method definition Expecting: but found instead : [51, 0] syntax error Expecting: kEND but found end-of-file instead org.jruby.parser.DefaultRubyParser$yyException: irrecoverable syntax error at end-of-file [...] > > obviously i've tried something like : > > [...] > > > > what is the ruby way for such nested methods ? > > Honestly, this code looks fine to me, assuming you definded > "doOperand()" and "doValue()" elsewhere in the script. Could you post a > little more code? :) the code (remember i'm using JRuby, not Ruby) : require 'java' module Swing include_package "javax.swing" end module JavaLang include_package 'java.lang' end me = $bsf.lookupBean ("me") tag = $bsf.lookupBean ("tag") textfield = me.getComponent("text-field") storage = me.getTagComponent("text-field") type = tag.getProperty("js-type") value = tag.getProperty("js-value") action = tag.getProperty("action") textfield.setText("From JRuby") print "type : ", type, "\n" print "value : ", value, "\n" print "action : ", action, "\n" print "action.length : ", action.length, "\n" text = textfield.getText() def clearField() textfield.setText("") end def doOperand(t, v) if value == "ce" clearField() else if value == "backspace" print "backspace()", "\n" else if value == "on" clearField() # setMemory("0") # setOperand("") # setProperty("action", value) end end def doIt(t, v) if t == "operand" doOperand(t, v) else print "doValue()", "\n" # doValue(t, v) end end doIt(type, value)
··· Mark Hubbart wrote: -- yt

Note the usage of my_type because type is already defined for each Ruby
object. But read on.

ok, i’ll change that !

what is the ruby way for such nested methods ?

First of all this doesn’t seem to be about nested methods (i.e. methods
defined in the body of other methods) but about invoking methods from other
methods but much more about dispatching according to certain criteria (the
object’s type in this case).

From what I see this looks like a typical case of method overloading, which
is one of the most important features in object oriented languages: Method
invocation is done depending on the type of instance at hand (this, btw, can
be done similarly in JavaScript):
[…]

tanks a lot 4 your advice, i want my code to be the most rubyich as
possible because it is only a demo for customers (ultrid supports many
script languages aside ruby : BeanShell DynamicJava Groovy Jacl
Javascript JRuby JudoScript Jython PNuts Rexx ) which might have
better knowledge upon Ruby as me…

when everything done i’ll repost d-the result in order to get critisism
about the rubyness of my code °;=)

···

Robert Klemme bob.news@gmx.net wrote:


yt

no change when using :

def doOperand(t, v)
if v == “ce”
clearField()
else if v == “backspace”
print “backspace()”, “\n”
else if v == “on”
[…]

···

Yvon Thoraval yvon.thoravalNO-SPAM@free.fr wrote:

def doOperand(t, v)
if value == “ce”
clearField()
else if value == “backspace”
print “backspace()”, “\n”
else if value == “on”


yt

def doOperand(t, v)
if value == “ce”
clearField()
else if value == “backspace”
print “backspace()”, “\n”
else if value == “on”
clearField()

setMemory(“0”)

setOperand(“”)

setProperty(“action”, value)

end
end

here is the prob
if you do:

if condition
stufff
else if ##nested check
else ##nested else
end ## just closing the internal if…else

you need to do:
if cind
stuff
elsif

elsif

else
end

···

il Sat, 20 Mar 2004 10:57:55 +0100, yvon.thoravalNO-SPAM@free.fr (Yvon Thoraval) ha scritto::

def doIt(t, v)
if t == “operand”
doOperand(t, v)
else
print “doValue()”, “\n”

doValue(t, v)

end
end

doIt(type, value)

Yvon Thoraval wrote:

def doOperand(t, v)
if value == “ce”
clearField()
else if value == “backspace”
print “backspace()”, “\n”
else if value == “on”

no change when using :

def doOperand(t, v)
if v == “ce”
clearField()
else if v == “backspace”

what about if you try
elsif v == “backspace”

elsif is the Ruby way of spelling else if, I assume JRuby is the same

   print "backspace()", "\n"

else if v == “on”
[…]

HTH

···

Yvon Thoraval yvon.thoravalNO-SPAM@free.fr wrote:


Mark Sparshatt

ok, tanxs !

···

gabriele renzi surrender_it@remove.yahoo.it wrote:

you need to do:
if cind
stuff
elsif

elsif

else
end


yt

that’s right, it works this way, tanxs !

···

Mark Sparshatt msparshatt@yahoo.co.uk wrote:

what about if you try
elsif v == “backspace”


yt