[Ruby-tut] It's Object-Oriented!
Author |
Message |
JHanson90
|
Posted: Mon Nov 01, 2004 10:39 pm Post subject: [Ruby-tut] It's Object-Oriented! |
|
|
I would like to make something clear to the people that read this forum everyday and take Ruby's uber-cool object-orientedness for granted. A little while back, I said that everything in Ruby was object-oriented. But what does that mean? Let me try to explain the best I can without [...] screwing it up.
code: | PHP:
<?php echo "This is" . " text."; ?> | In that code fragment, "echo" is a "contruct" of the PHP language, and the '.' brought the two strings together. What does that mean? In Ruby, there really is no ambiguity: code: | # Ruby
puts "This is " + "text."
| That is equivalent to the following (though it might return an error because of Ruby coding standards): code: | self.puts("This is ").+("text.")
| Why? Because, unlike in many other languages where functions are just functions and operators are just operators, everything in Ruby is object-oriented; "puts" is a method of the "self" object (self being the actual program), and "+" is a method that adds its argument to the previous expression.
The most strict idea that everything is OO in Ruby exists; it suggests the most structured foundation for a great, powerful, but easy language that I hope will grow in the Western hemisphere.
On with a Simple Ruby Tutorial.
You will need a Ruby interpreter -> http://www.ruby-lang.org/
> Comments... code: | # this is a Ruby comment, lasts till end of da line |
> Local variables and method names must begin with a lowercase letter.
> There are four important arithmetic methods (these are similar, but not identical, to "operators" in other languages): +, -, *, /. Here are explanations of + and *, some useful ones that differ from other languages:
+ -- This will combine any two expressions of the same type. Methods to ensure they're the same type are explained later. code: | variable = 5 + 5 # that expression returns 10
variable1 = "super" + "dee" + "duper" # that expression returns string "superdeeduper" |
* -- This will multiply two expressions, but that don't necessarily have to be of the same type. code: | variable2 = "A B C " * 2 # that expression will return string "A B C A B C "
variable3 = 2 * "A B C " # this will return an error. Unlike above, where you could say A B C teice, you cannot say 2 for an "A B C " amount of times. |
> No need for semicolons at the end of each statement, so long as there is still a newline character there.
> Use the "to_s", "to_i", and "to_f" methods to convert their object to the string, integer, and float data types, respectively. code: | variable4 = '5' # string
variable5 = 5 # integer
variable6 = 5.0 # floating point
variable7 = variable4.to_i + variable5 + variabke7.to_i # that expression returns 15, but...
variable8 = variable4 + variable5.to_s + variable6.to_s # that expression returns string "555" |
> If, elsif, else just like in other languages. Use == method for equality and != method for inequality, etc. Use "and", "or", and "not" 'logical' methods (like logical operators... ) code: | if (variable9 == variable8 or not variable9 == variable7)
# do something
elsif (5 == var)
# do something else
else
# otherwise do this
end |
> Use gets for taking input from the user's keyboard, and it would be a good idea to 'chomp' off the newline character at the end of it too. code: | variable10 = gets.chomp # get just means 'get', and s means 'string', hence gets or 'get string' |
> To define your own methods use the def keyword code: | def a_method a_parameter
puts a_parameter
"this is what gets returned; it is the last line of code in the method"
end
variable11 = a_method "Ruby is cool" # declare variable 11 is assigned to the return value of the 'a_method' method, with the parameter "Ruby is cool"
puts variable11 # output: "this is what gets returned; it is the last line of code in the method" (puts returns nil, the no-value value in Ruby, so puts a_parameter in the method definition didn't do anything) |
A Program code: | #!/usr/bin/ruby
# you can use shebeng and make the file executable on linux, to escape the idea of calling the ruby interpreter yourself
def say_hi
"hi"
end
puts "PROGRAM: You suck at Ruby."
print "YOU: "
your_response = gets.chomp
if your_response.downcase == 'so do you' # the 'downcase' method converts its object to all lowercase, but doesn't affect that direct variable.
puts "PROGRAM: Yeah... well, how about I say hi?"
puts say_hi
else
puts "PROGRAM: I'm smart cause int 5 + int 5 = 10 but string 5 + string 5 = 55 :)"
end |
I am very error-prone; if I made any mistakes, wtd, go ahead and point them out |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Mon Nov 01, 2004 11:00 pm Post subject: (No subject) |
|
|
Very nice.
Have some bits.
Now show people how to create their own classes. |
|
|
|
|
|
Tony
|
Posted: Mon Nov 01, 2004 11:38 pm Post subject: (No subject) |
|
|
great to see more people interested in Ruby so much Keep it up. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
|
|