中整個早上都忙著作業,看來是假期懶了一下現在現眼報吧哈哈。在上課之前發一下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. Swift数组的存取与修改

    对数组的存取与修改可以通过数组的方法和属性来进行,或者使用数组的下标语法. 要知道数组中元素的数量,可以查看它的只读属性count: println("The shopping list c ...

  2. 【Android Developers Training】 28. 将用户带领到另一个应用

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. workday1

    前天是实习的第一天,现在补下感想 总的来说还是不错的,师兄很nice,师妹很羞涩,我很尴尬,我的交际能力还是有待提高(主要是普通话不标准~~~~(>_<)~~~~) 早上由华工C12穿梭到 ...

  4. WCF学习——构建第二个WCF应用程序(六)

    一.创建客户端应用程序 若要创建客户端应用程序,你将另外添加一个项目,添加对该项目的服务引用,配置数据源,并创建一个用户界面以显示服务中的数据.若要创建客户端应用程序,你将另外添加一个项目,添加对该项 ...

  5. Vue中应用CORS实现AJAX跨域,及它在 form data 和 request payload 的小坑处理

    基本概念部分(一):理解CORS 说道Vue的跨域AJAX,我想先梳理一遍CORS跨域,"跨域资源共享"(Cross-origin resource sharing),它是一个W3 ...

  6. Linux之用户管理--初级上

    管理用户命令汇总 命令 注释说明(特殊颜色的必须掌握) useradd增 同adduser命令,执行此命令可在系统中添加用户.(更改4个用户文件) userdel删 执行此命令可删除用户及相关用户的配 ...

  7. 深入理解Java虚拟机-----------虚拟机类加载机制

    虚拟机类加载机制 类从被加载到虚拟机内存开始,到卸载出内存为止,整个生命周期包括:加载,验证,准备,解析,初始化,使用,卸载等7个阶段.其中,验证,准备,解析3个部分称为连接. 以上7个阶段中,加载, ...

  8. Microsoft office2010页码设置----论文、课程设计报告格式

    思想:将目录页(含目录页)与目录页以下的页面用分隔符分隔开,单独设置目录页以下的页面页码,删除目录页(含目录)以前的页码. 1.在目录页页面内容最下面一行插入分隔符,实现与下面页面分隔开的目的. 页面 ...

  9. Javasript 正则匹配任意字符

    今天在写Js匹配任意字符的时候发现使用.不好使,当输入内容有回车的时候就会失效. 后来上网查,发现js的任意字符不包括\n. 本来想写一个trim后长度为10的正则验证,最后使用[\s|\S]来代替任 ...

  10. 用户代理字符串(navigator.userAgent)检测方法

    最近在看<JavaScript 高级程序设计(第三版)>,发现其中关于用户代理字符串检测技术的一些方法,觉得讲的很详细.用户代理字符串(navigator.userAgent)中包含了大量 ...