Page 1 of 1

Ruby code help

Posted: Tue Apr 23, 2013 8:06 pm
by Yuroun
I'm trying to learn Ruby a bit. I'm trying to convert my old Julian date routine from Pascal to Ruby, but get a error message.

This gives error message: "Unexpected tIDENTIFIER, expecting keyword_end"

I don't know why :roll:

Code: Select all

Temp  = (@Month - 14) div 12
Y = @Year
M = @Month
D =@Day
T1 = 1461 * (Y + 4800 + Temp) div 4
T2 = 367 * (M - 2 - Temp * 12) div 12
T3 = 3 * (Y + 4900 + Temp) div 100
T4 = T3 div 4
output   D - 32075 + T1 + T2 - T4

Re: Ruby code help

Posted: Tue Apr 23, 2013 8:21 pm
by trogluddite
Hi Yaroun,

It's the capital letters used for your variable names that Ruby doesn't like - it's very strict about that kind of thing, because caps/lower-case has special meaning in Ruby's syntax. The rest of your code looks fine.
Variable names should always start with a lower-case letter - the convention among 'Rubyists' is that every letter is lower-case, but the first letter is especially important.

An identifier with a capital letter at the start is taken to be the name of a Class, Module or Constant - rather annoying really, as caps/not-caps is way the easiest sort of typo to make when you're coding in a hurry!

Re: Ruby code help

Posted: Tue Apr 23, 2013 8:44 pm
by Yuroun
trogluddite wrote:Hi Yaroun,

It's the capital letters used for your variable names that Ruby doesn't like - it's very strict about that kind of thing, because caps/lower-case has special meaning in Ruby's syntax. The rest of your code looks fine.
Variable names should always start with a lower-case letter - the convention among 'Rubyists' is that every letter is lower-case, but the first letter is especially important.

An identifier with a capital letter at the start is taken to be the name of a Class, Module or Constant - rather annoying really, as caps/not-caps is way the easiest sort of typo to make when you're coding in a hurry!


Hmmm, that's not the problem. I used lowercase for all variables, and Ruby is still complaining about line 7 "temp3 = 3 * (y + 4900 + temp) div 100"

Here's the schematic if someone want's to look at it. I'm lost for today :lol:

JulianDate2.fsm
(412 Bytes) Downloaded 1104 times

Re: Ruby code help

Posted: Tue Apr 23, 2013 10:35 pm
by trogluddite
D'oh, sorry Yaroun, :oops:
I missed the other mistake staring me in the face - it's the way that 'div' has been used.
There are two ways to fix this one.

1) Replace 'div' with '/' - so long as the numbers either side of the divide are integers, the result will also be an integer...
JulianDate2-fix2.fsm
(420 Bytes) Downloaded 993 times


2) 'div' is a 'method'', not an operator in the sense that most other languages would use it.

This fix (and that sentence!) will probably seem very strange at first to someone coming from Pascal - but it shows a key part of Ruby syntax that will become very important...
JulianDate2 - fix.fsm
(430 Bytes) Downloaded 975 times

You'll see here that I've kept the 'div' but I've bracketed together the numbers, and put a dot between the first set of numbers and the 'div'.
This is the usual way of doing things in Ruby most of the time - things like '+', '=' etc. that don't use the 'dot' notation, are actually special cases that are included to make writing maths a little bit easier ("syntax sugar")

The general way of doing something to an object (variable, value) is...
this_object.do_something(parameters)
"this_object" is the thing you want to do something to - i.e. in this case the 'temp' stuff.
"do_something" after the dot is the action that you want to perform - e.g. 'div'
"(parameters)" are any other values that are needed to work out how to do the "do_something" - e.g. the number you want to divide by.

For example, to round a number to two decimal places, you would write...
rounded = my_number.round(2)

In most other languages, you'd probably think of this as something like calling a function or procedure - in Ruby, they are called 'methods', and every single thing you can do to a value is really a method. But there's a slight difference...
A function call in most languages looks something like...
a = div(x,y)
...whereas in Ruby, it would be...
a = x.div(y)
The best way that I've heard this explained is to think of methods as a message that you send to something - "tell x to use its 'div' method with 'y' as the divisor". When reading about Ruby programming you will often see that first value 'x' referred to as the "receiver", because it's the one receiving the "do this now" messages.

Just to be even more confusing, the brackets around the parameters are usually optional...
rounded = my_number.round 2
divided = my_number.div 2
Personally, I nearly always use the brackets, and most 'Rubyists' recommend this, because it makes the syntax clearer - just using spaces can make it unclear where values, methods and parameters begin and end, and can even confuse the parser sometimes.

(NB - I love that DSPr included Ruby, but their examples show a whole load of dodgy ways of writing it that the Ruby 'style' standards recommend you don't use!).

If that all seems very confusing, don't worry, you are not alone. I come from a procedural language background too, and all this "object oriented" stuff still seems very weird!

Re: Ruby code help

Posted: Tue Apr 23, 2013 10:53 pm
by Yuroun
trogluddite wrote:If that all seems very confusing, don't worry, you are not alone. I come from a procedural language background too, and all this "object oriented" stuff still seems very weird!


Hmm, I understand now why my example doesn't work. Thanks for this learning experience :P

next time I use the .div ;)

It's a bit strange for me to use it this way, although I'm used to object oriented programming. But I know now it's part of the syntax. I get used to it ;)

Again, thanks for helping me out.