Javascript vs. Ruby - how would this look in Ruby?

I looked at some javascript code lately:

TimeSeries.prototype.append = function(timestamp, value) {
this.data.push([timestamp, value]);
this.maxValue = !isNaN(this.maxValue) ? Math.max(this.maxValue, value)
: value;
this.minValue = !isNaN(this.minValue) ? Math.min(this.minValue, value)
: value;
};

And I thought the first line is quite nice (the explicit this. kinda
sucks though).

My question is:

How would the above code look in Ruby?

TimeSeries.prototype.append = def timestamp(value) {
@data.push([timestamp, value])
@maxValue = !isNaN(@maxValue) ? Math.max(this.maxValue, value) : value
@minValue = !isNaN(@minValue) ? Math.min(this.minValue, value) : value
};

Obviously I missed a few things, of course this is not valid ruby code.

I am just wondering what the equivalent would be in ruby.

Can it be that javascript is initially TERSER than ruby here?

Hi,

You shouldn’t try to translate code word by word. This simply does not
work (just like with natural languages).

First of all, Ruby uses class based inheritance and not prototype bases
inheritance. So instead of adding a method to the prototype of
TimeSeries, you would open the class TimeSeries and define a new method.
Defining a method is also completely different in JavaScript and Ruby.
In JavaScript, you create a function object and store it in an attribute
of the object. In Ruby, you simply write a method definition:

class TimeSeries
def append timestamp, value
@data << [timestamp, value]
@max_value = value unless @max_value and @max_value >= value
@min_value = value unless @min_value and @min_value <= value
end
end

Personally I find this much more intuitive and simple than the
JavaScript code.

Hmm. I like ruby’s syntax much more than JS, but I kinda
have to admit, the:

TimeSeries.prototype.append = def timestamp(value) {

Isn’t as ugly as the typical Javascript code I look at.
And the ruby version seems at least as verbose as the
JS code.

On Saturday, March 17, 2012 5:08:24 PM UTC-7, Jan E. wrote:

class TimeSeries
def append timestamp, value
@data << [timestamp, value]
@max_value = value unless @max_value and @max_value >= value
@min_value = value unless @min_value and @min_value <= value

@max_value = [@max_value, value].compact.max
@min_value = [@min_value, value].compact.min

end