If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
Ruby code help
5 posts
• Page 1 of 1
Ruby code help
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
This gives error message: "Unexpected tIDENTIFIER, expecting keyword_end"
I don't know why
- 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
-
Yuroun - Posts: 27
- Joined: Mon Dec 19, 2011 7:32 pm
- Location: Tilburg, The Nethlands
Re: Ruby code help
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!
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!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: Ruby code help
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
-
Yuroun - Posts: 27
- Joined: Mon Dec 19, 2011 7:32 pm
- Location: Tilburg, The Nethlands
Re: Ruby code help
D'oh, sorry Yaroun,
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...
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...
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!
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...
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...
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!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: Ruby code help
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
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.
-
Yuroun - Posts: 27
- Joined: Mon Dec 19, 2011 7:32 pm
- Location: Tilburg, The Nethlands
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: Google [Bot] and 88 guests