中整個早上都忙著作業,看來是假期懶了一下現在現眼報吧哈哈。在上課之前發一下Ruby 的首章,算是倉促的開始吧。

puts

puts "Once upon a time...

there's a self learn programmer...."   #okay

puts

"Once upon a time...

there's a self learn programmer...."  # output: *blank*

所以Ruby 是case-sensitive 咯?但在網上卻是這樣說:“Whitespace characters such as spaces and tabs are generally ignored in Ruby code, except when they appear in strings. ”(Tutorial Point)。所以,我們推斷 Ruby command 是不可以被分開的。

puts vs print

puts "Hello World"  #auto-append "\n"

print "I am in "    #not appending "\n"

print "campus"

output:

Hello World

I am in campus

啊,要去上課了,待續...

https://www.tutorialspoint.com/ruby/ruby_comments.htm

BEGIN{} & END{} Priorities

puts "in the middle, even written in front?! "

BEGIN{
puts "prog initialize"
}

END{
puts "just before prog termination"
}

output:

prog initialize

in the middle, even written in front?!

just before prog termination

Block commenting

=begin
block comment
instead of using #
=end

Class in Ruby

#in Ruby, everything treated as object

 #in Ruby, everything treated as object 

 $ monthly_customers = 0; 

 class Customer
     @@no_of_customers = 0;         #class var: static DM: all obj share 1 copy

     #member function in ruby class
     def initialize(id, name, addr)    #initialize: the reserved method for constructors
 #lowercase letters for method name
     @cust_id = id    #instance var == DM
     @cust_name = name
     @cust_addr = addr
     end 

     def modify_addr(addr2)
     ::Customer.instance_variable_get(:@cust_addr) = addr2    #modify instance var in same class
     end 

 end

 cust1 = Customer.new(", "Amy", "CauswayBay, HK")
 cust2 = Customer.new;
 p cust2.instance_variable_get(:@cust_id)    #nil: access uninitialized instance variable
 cust2.initialize(", "Timonthy", "Kwai Shing")
 p cust2.instance_variable_get(:@cust_id)    # "02"

差不多要上課了,待續...

https://www.tutorialspoint.com/ruby/ruby_variables.htm
https://www.tutorialspoint.com/ruby/ruby_class_case_study.htm

Global variable in Ruby

$global_variable = "Hello kitty"  #define a global var
class Class1
  def print
    puts "Global variable is: #$global_variable"    #access global var
  end
end   #to finish class definition

class1obj = Class1.new
class1obj.print   #no global fnc in ruby: fnc involved by obj only

Local variable, constant & execution order

$global_variable = "Hello kitty"  #define a global var
class Class1
  @@object_Counter = 1    #no space btw '@@' and identifier name
  FIXED_VALUE = 100     #const var starts with capital, local var starts with lower case 

  def print
    @local_var = "Hello global"
    puts "Global variable is: #$global_variable"    #access global var
    puts "Instance varable ~ DM: #@local_var"     #access instance var
    puts "Existing objects = #@@object_Counter"
  end

  puts "when local variable out of scope: #@local_var"
  # codes defined out of any class method executed first: ~ global MF

  puts "The constant in class : #{FIXED_VALUE}"     #access constant  

end   #to finish class definition

class1obj = Class1.new
class1obj.print   #no global fnc in ruby: fnc involved by obj only

output:

when local variable out of scope:
The constant in class : 100
Global variable is: Hello kitty
Instance varable ~ DM: Hello global
Existing objects = 1
=> nil

to be continued...

Ruby01: Beginner的更多相关文章

  1. A Beginner's Guide to Paxos

    Google Drive: A Beginner's Guide to Paxos The code ideas of Paxos protocol: 1) Optimistic concurrenc ...

  2. Beginner's Guide to Python-新手指导

    Refer English Version: http://wiki.python.org/moin/BeginnersGuide New to programming? Python is free ...

  3. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之纹理Textures

    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之纹理Textures 本篇分享一下第6个已完工的视频,即<beginner Graphics ...

  4. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格Meshes

    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格Meshes 本篇分享一下第5个已完工的视频,即<beginner Graphics – ...

  5. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials

    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials 既上一篇分享了中文字幕的灯光介绍Lights后,本篇分享一下第3个已完工 ...

  6. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之灯光介绍Lights

    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之灯光介绍Lights 既上一篇分享了中文字幕的摄像机介绍Cameras后,本篇分享一下第2个已完工的 ...

  7. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras

    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras 最近得到一些Unity官方视频教程,一看全是纯英文的讲解,没有任何字幕或者 ...

  8. 翻译:Lisp Style Tips for the Beginner - Heinrich Taube

    原文:Lisp Style Tips for the Beginner 本篇文章是一篇非正式的摘要,旨在帮助新手写出高效.易读的Lisp代码. 1 赋值   1.1 避免使用eval.赋值是Lisp内 ...

  9. A Beginner's Guide To Understanding Convolutional Neural Networks(转)

    A Beginner's Guide To Understanding Convolutional Neural Networks Introduction Convolutional neural ...

随机推荐

  1. Yii 中出现“<?= ... ?>”是什么意思?

    这是php的标签,和<?php ?>一样的.用<?= ?> 就不用echo,否则你要输出的话,需要加上echo词汇输出.这个就是用在模板里面方便点.

  2. 用超链接a来提交form表单

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  3. css之选择器总结

    首先我们来看下有哪些选择器??? 一.基础选择器: html标签选择器:通过html标签来选择元素. ①所有的html标签都可以当做选择器. ②无论标签藏多深都会被选中. ③选择的是所有的标签而不是某 ...

  4. 2017全球互联网技术大会回顾(附PPT)

    有幸遇见 GITC2017上海站,刚好遇见你! 为期两天(6.23~24)的GITC大会在上海举行,我有幸参加了24号的那场,也就是上周六,之所以今天才来回顾,是我想等PPT出来后分享给大家! 这应该 ...

  5. 【LeetCode】112. Path Sum

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  6. 机器学习之分类问题实战(基于UCI Bank Marketing Dataset)

    导读: 分类问题是机器学习应用中的常见问题,而二分类问题是其中的典型,例如垃圾邮件的识别.本文基于UCI机器学习数据库中的银行营销数据集,从对数据集进行探索,数据预处理和特征工程,到学习模型的评估与选 ...

  7. Java添加JDBC

    添加JDBC 1.SQL Server SQL Server2005 下载 sqljdbc_4.0 https://www.microsoft.com/en-us/download/details.a ...

  8. Javacript 学习笔记

    一.初探 javacript 学习无法是围绕着对象和属性两个方面来兜圈子,万变不离其宗. 在js中,能点出来的,或者中括号里面的必然是属性(方法).数组除外. 对象调用属性! 对象调用属性! 对象调用 ...

  9. 数位DP练习

    水题 发布时间: 2017年6月22日 19:15   最后更新: 2017年6月23日 20:10   时间限制: 1000ms   内存限制: 128M 描述 给一个数n,求0~n内有多少个数满足 ...

  10. Java异常体系简析

    最近在阅读<Java编程思想>的时候看到了书中对异常的描述,结合自己阅读源码经历,谈谈自己对异常的理解.首先记住下面两句话: 除非你能解决这个异常,否则不要捕获它,如果打算记录错误消息,那 ...