Ruby

From Devpit
Jump to: navigation, search

Ruby is an object oriented scripting language that borrows some of its syntax from perl. Its defining features seems to be Rails

Features

  • The line break is a newline
  • The end of a method/class.. is marked be the keyword 'end'
  • method names can end in ?, =, and !

Example

#!/usr/local/bin/ruby

def say_hello(name)
   result = "Hello " +name
    # this is not needed it would return result by default
   return result   
end

puts say_hello("World")

#Classes must be capitalized
class Say
  def initialize(name)
    @name = name
  end
  def hello
    result = "Hello " + @name
  end
  def goodbye
    result = "Goodbye " + @name
  end

# called as Say.name = "new Name"
  def name=(new_name)
    @name = new_name
  end
end


world = Say.new("world")
puts world.hello
puts world.goodbye
world.name = "Earth"
puts world.hello   

And the result

Hello World
Hello world
Goodbye world
Hello Earth     


See Also

External Links

Ruby-lang.org