ruby notes (1)

  • everything is an object
  • puts = put string
  • Java and C programmers – no need to write a main method/function
  • String literals are sequences of characters between single or double quotation marks. I am using single quotes around Hello. ‘ is more efficient than ” for constant string
  • Ruby is an interpreted language, so you don’t have to recompile to execute the program written in Ruby
  • The Ruby coding convention states that file/directory name is lower case of class/module name with .rb extension. For example, Foo class has name foo.rb
  • Multiple statements on one line must be separated by semicolons, but they are not required at the end of a line;
  • If a line ends with a backslash (\), the linefeed following it is ignored; this allows you to have a single logical line that spans several lines
  • everything is true except the reserved words false and nil
  • An integer literal is simply a sequence of digits eg. 0, 123, 123456789. Underscores may be inserted into integer literals (though not at the beginning or end), and this feature is sometimes used as a thousands separator eg. 1_000_000_000.
  • The increment and decrement operators (++ and – -) are not available in Ruby, neither in “pre” nor “post” forms. However, do note that the += and -= are available.
  • some operators like =,>=, <=, +, – , <<, >> is a kind of syntactic sugar (more on this later) – where something looks like an operator but is a method call.
  • Both or and || return their first argument unless it is false, in which case they evaluate and return their second argument. The only difference between or and || is their precedence. || has a higher precedence than or
  • “and” and “or” has a lower precedence than the assignment too, so we can write

if a = f(x) and b = f(y) and c = f(z) then d = g(a,b,c) end

which computes the value of d unless any of the values f(x),f(y),f(z) is false

  • use back-tick (`) in puts, as puts `dir` will show the directory of current dir
  • Every class or module definition block (class, module) has its own local scope, even nested class/module definition blocks.
  • Class variables are rarely used in Ruby programs.
  • A constant name starts with an uppercase letter followed by name characters. Class names and module names are constants
  • ruby is dynamic, fixed type is not required for a variable

x = 7           # integer

x = “house”  # string

  • FTE=full time employee
  • json string is a string, so it cannot be nil.

=> “\”null\””
=> “null”

  • ruby allow us to write functions that can accept variable number of parameters
def foo(*my_string)
puts foo(‘hello’,’world’)
puts foo()

The asterisk (called the splat argument) is actually taking all arguments you send to the method and assigning them to an array named my_string. As you can see, by making use of the asterisk, we’re even able to pass in zero arguments. The code above will result in the Array [‘hello’, ‘world’] written in the first method call and an empty Array being written on the second call, as you can see in the following output:

[“hello”, “world”]
[ ]
  • the parameters passed by reference/address, everything is reference but not the objects themselves

# examples
def mymethod a
mystr = “abc”
mymethod(mystr) => “ABC”
mystr => “abc”

def mymethod2 b
mymethod2(mystr) => “ABC”
mystr => “ABC”
  • Ruby methods that modify an object in-place and end in an exclamation mark are known as bang methods. Examples of such pairs of methods include sort/sort! for arrays, upcase/upcase! for strings, chomp/chomp! for strings, and reverse/reverse! for strings and arrays. In each case, if you call the non-bang version of the method on the object, you get a new object. If you call the bang version, you operate in-place on the same object.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s