Skip to content

Latest commit

 

History

History
94 lines (70 loc) · 3.92 KB

README.md

File metadata and controls

94 lines (70 loc) · 3.92 KB

ruby-gold-notes

  1. Subtraction between Date classes is Rational Other similar classes of operations are summerized below:
  • Subtraction between Time classes is Float
  • Subtraction between DateTime classes is Rational
  1. Options available in Ruby
  • -l : Execute String#chop! at the end of each line
  • -n: cases Ruby to assume the following loop around your script, which makes it iterate over filename arguments somewhat like sed -n or awk
  • -p: Similar to -n but output $_
  • -t, -f: Not support
  • -d: turns on debug mode, $DEBUG will set true
  • -r filename: causes Ruby to load the file using require. It is useful with switches -n or -p References: options
  1. Object#methods
  • returns a list of the names of public and protected methods of obj. This will include all the methods accessible in obj's ancestors. If the optional parameter is false, it returns an array of obj's public and protected singleton methods, the array will not include methods in modules included in obj
  • Syntax:
methods(regular=true) -> array
  1. Module#using cannot be called inside a method. If called, a RuntimeError will occur.

  2. Proc#yield is the alias of Proc#call. So we can use each either

yield args

or

block.call(args)

or

block[args]

or

block.yield(args)
  1. If the :: operator appears at the beginning, constant search is performed from the top level.

  2. BasicObject#method_missing is called when a method is not found after following the inheritance chain. BasicObject#method_missing also follows the inheritance chain.

  3. % notation % is called % notation.

  • %a/ /: There is no such notation
  • %/ /: double quote string
  • %r/ /: regular expression`
  • %w/ /: array whose elements are strings
  • Please refer to the Ruby reference for detailed explanation.
  1. Public Object#instance_variables: returns an array of instance variable names for the receiver. Note that simply defining an accessor does not create the corresponding instance variable.

  2. Module#alias_method

  • Makes new_name a new copy of the method old_name. This can be used to retain access to methods that are overridden
  • Syntax:
alias_method(new_name, old_name) -> symbol
  • Note: alias_method will create copies of the old_name method. If you modify the old_name method after aliasing. the new_name method will remain unchanged.
  1. A variadic function
  • In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages
  1. Note about Object#dup, Marshal.dump and Object#copy
  • Object#dup doeas not copy an object's singleton methods.
  • Marshal.dump is unable to serialize objects that have singleton methods defined on them
  • There is no Object#copy method
  1. Ruby case..when case..when

  2. Only date library has the Date and DateTime object, and only DateTime has method now. Time has method now too, but the result is Float, not Rational

  3. Top-level definition method is private method of Object

  4. The ancestry chain for each of the classes listed in this question is as follows

ArgumentError < StandardError < Exception

ScriptError < Exception

If rescue is called without a specific error class, it will catch StandardError and its descendents by default. Most exceptions in core Ruby are descendents of StandardError, but there are some that are not usually meant to be rescued which exist in other class hierarchies which descend directly from the Exception base class.

  1. Casting
a = 1.0 + 1 # Float
a = a + (1/2r)	# Float
a = a + (1 + 2i)	# Complex