View Full Version : Ruby: I'm doing something stupid.
theone3
06-25-2006, 07:40 AM
I Decided to try and learn programming with Ruby. I'm only getting going, so I assume this question is very simple.
Trying to run this script closes ruby before I can get an error message!
def leapcheck year
if year.%(4) == 0
if year.%(100) != 0
#disible by 4 [good]
#not divisible by 100 [good]
leapyear = true
else
if year.%(400) == 0
#disible by 4 [good]
#divisible by 100, but also divisble by 400 [okay]
leapyear = true
else
#disible by 4 [good]
#divisible by 100, but not divisble by 400 [bad]
leapyear = false
end
end
else
#not divisible by 4
leapyear = false
end
leapyear #return this
end
puts 'press any key...'
b = gets
1990check = leapcheck 1990
1900check = leapcheck 1900
2000check = leapcheck 2000
1984check = leapcheck 1984
2004check = leapcheck 2004
puts 1990check
puts 1900check
puts 2000check
puts 1984check
puts 2004check
puts
puts 'press any key...'
a = gets
You don't even the last line in your procedure "leapyear", since it returns the last value calculated, which would be the boolean values you assign to the variable. :)
As for it closing, it's a command-line based program, so you have to open a command-line first, then run it from there:
Start->Run->`cmd` (Command-Shift-U from Finder, then Terminal.app)
Then `ruby myfile.rb`. The window will then stay open after script execution.
theone3
06-25-2006, 04:03 PM
You don't even the last line in your procedure "leapyear", since it returns the last value calculated, which would be the boolean values you assign to the variable. :)
As for it closing, it's a command-line based program, so you have to open a command-line first, then run it from there:
Start->Run->`cmd` (Command-Shift-U from Finder, then Terminal.app)
Then `ruby myfile.rb`. The window will then stay open after script execution. OK, I ran it through command prompt, and now I'm getting a syntax error on line 30:
1990check = leapcheck 1990
.........^
any ideas?
TheShaner
06-26-2006, 02:52 AM
I don't program in Ruby, but like all programming languages (which should include Ruby), your variables cannot start with a number. They must start with a letter.
Try this instead:
check1990 = leapcheck 1990
check1900 = leapcheck 1900
check2000 = leapcheck 2000
check1984 = leapcheck 1984
check2004 = leapcheck 2004
puts check1990
puts check1900
puts check2000
puts check1984
puts check2004
-Shane
theone3
06-26-2006, 05:48 AM
That was it! It works now!
Danke Shane! (Thanks)
]|V|[agnus
06-26-2006, 04:32 PM
I am about a year into my Ruby studies, and I'm glad to finally see some Ruby threads popping up on CodingForums!
Some coding style convention suggestions(I've added comments to your original paste):
def leapcheck year
Though it is legal not to put parens around your method arguments, most at least include them for the original method definition. I follow this convention because I think it helps clarify method definitions when scanning code.
def leapcheck(year) ...
if year.%(4) == 0
Here is a situation where I think you should utilize Ruby's syntactic sugar:
if (year % 4) == 0
I think the parens around the modulus expression might be necessary because Ruby might interpret "year % 4 == 0" as "year % false". Try it out, though.
...
All things considered, there's another big thing you should consider here: the open nature of Ruby. You could add a methods to Date or Time(or both! or maybe you just add it to Fixnum?), for instance. What if you could simply say:
Date.parse(2004).leap_year?
Maybe I'll work that out later today.. gotta focus on other stuff at the moment. ;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.