Ruby01: Beginner
中整個早上都忙著作業,看來是假期懶了一下現在現眼報吧哈哈。在上課之前發一下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的更多相关文章
- A Beginner's Guide to Paxos
Google Drive: A Beginner's Guide to Paxos The code ideas of Paxos protocol: 1) Optimistic concurrenc ...
- Beginner's Guide to Python-新手指导
Refer English Version: http://wiki.python.org/moin/BeginnersGuide New to programming? Python is free ...
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之纹理Textures
[我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之纹理Textures 本篇分享一下第6个已完工的视频,即<beginner Graphics ...
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格Meshes
[我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格Meshes 本篇分享一下第5个已完工的视频,即<beginner Graphics – ...
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials
[我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials 既上一篇分享了中文字幕的灯光介绍Lights后,本篇分享一下第3个已完工 ...
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之灯光介绍Lights
[我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之灯光介绍Lights 既上一篇分享了中文字幕的摄像机介绍Cameras后,本篇分享一下第2个已完工的 ...
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras
[我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras 最近得到一些Unity官方视频教程,一看全是纯英文的讲解,没有任何字幕或者 ...
- 翻译:Lisp Style Tips for the Beginner - Heinrich Taube
原文:Lisp Style Tips for the Beginner 本篇文章是一篇非正式的摘要,旨在帮助新手写出高效.易读的Lisp代码. 1 赋值 1.1 避免使用eval.赋值是Lisp内 ...
- A Beginner's Guide To Understanding Convolutional Neural Networks(转)
A Beginner's Guide To Understanding Convolutional Neural Networks Introduction Convolutional neural ...
随机推荐
- 中奖概率算法(php 可用于刮刮卡,大转盘等抽奖算法)
<?php //中奖概率算法(php 可用于刮刮卡,大转盘等抽奖算法) /* * 经典的概率算法, * $proArr是一个预先设置的数组, * 假设数组为:array(100,200,300, ...
- Disruptor的应用示例——大文件拆分
结合最近Disruptor的学习,和之前一直思考解决的大文件拆分问题,想到是否可以使用Disruptor作为生产者/消费者传递数据的通道呢?借助其高效的传递,理论上应当可以提升性能.此文便是此想法的落 ...
- [图形学] Chp18 OpenGL表面纹理函数
以2D表面为例展示纹理贴图,用opengl设置一个2D纹理,颜色存储在32*32*3的数组中,对应的纹理坐标为0<=s, t<=1.0. 画出几个正方形表面,分别以GL_CLAMP(纹理坐 ...
- 基于 svn 服务器及 cocoapods-repo-svn 插件进行组件化私有库的创建
一.准备 组件化 随着业务需求的增长,在单工程 MVC 模式下,app 代码逐渐变得庞大,面对的高耦合的代码和复杂的功能模块,我们或许就需要进行重构了,以组件化的形式,将需要的组件以 pod 私有库的 ...
- Chrome浏览器扩展开发系列之四:Browser Action类型的Chrome浏览器扩展
Browser Action类型的Google Chrome扩展程序,通常在Chrome浏览器的工具栏中,地址栏的右侧,有一个始终存在的图标.也就是说,这个图标与浏览器相关,只要安装了该Chrome扩 ...
- java 网络编程 UDP TCP
网络编程 网络编程主要用于解决计算机与计算机(手机.平板..)之间的数据传输问题. 网络编程: 不需要基于html页面就可以达到数据之间的传输. 比如: feiQ , QQ , 微信....网页编程: ...
- Django学习(二)---使用模板Templates
学会使用渲染模板的方法来显示html内容. 一.Templates是什么: HTML文件 使用了Django模板语言(Django Tamplate Language DTL) 可以使用第三方模板 二 ...
- 【亲测】Appium测试Android混合应用时,第二次切换到WebView失败
要解决的问题:Appium测试Android混合应用时,第二次切换到WebView时失败 原因分析:在用Appium测试Android混合应用时,当程序第一次切换到WebView时,可以正常进行自动化 ...
- .net 中的相等性比较
引用相等性和值相等性 在 C# 中,相等性分为引用相等性和值相等性.引用相等性是指,若两个引用类型的变量引用的是同一个对象,则它们具有引用相等性. // x, y, z 都是引用类型变量 object ...
- vim的tab键设定
多在windows上编程的童鞋可能习惯于感受tab键为4个空格的长度,不过在linux系统中一般默认设定tab键为8个空格长度来显示.事实上tab也确实是8个空格的长度.不过由于习惯问题,某些童鞋还是 ...