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 ...
随机推荐
- 使用flask开发网站后端
Flask 是一个用于 Python 的微型网络开发框架,可以用于快速的搭建一个小型的网站. 我的搜索引擎:http://www.abelkhan.com 就是基于flask开发 一个flask的He ...
- 【Android Developers Training】 31. 序言:共享简单数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 2.如何安装vmvare tools
1.在主页点击虚拟机 重装vmvaretools,接着就会下载tar.gz包 2.cd 到解压包的地方,解压sudo tar zxf ... 3.解压之后会生成一个vmvare-toos-distri ...
- Ionic开发笔记
Ionic 开发笔记 记录开发中遇到的一些问题 ion-side-menu,使所有顶部导航标题居中 <!-- 添加 align-title="center" 使顶部导航标题居 ...
- Python的语言类型
Python 是强类型的动态脚本语言 . 强类型:不允许不同类型相加 动态:不使用显示数据类型声明,且确定一个变量的类型是在第一次给它赋值的时候 脚本语言:一般也是解释型语言,运行代码只需要一个解释器 ...
- 二、redis集群搭建
redis集群搭建 redis3.0后支持集群.集群中应该至少有三个节点,每个节点有一备份节点.需要6台服务器.搭建伪分布式,需要6个redis实例.搭建集群的步骤: 一.安装单机版redis 第一步 ...
- java 数据库编程 学习笔记 不断更新
最近开始学习java,感觉java的数据库编程需要发个随笔记录一下,话不多说 切入正题. 一.数据库访问技术的简介 应用程序 → 执行SQL语句 →数据库 → 检索数据结果 → 应用程序 ( ...
- Java Web使用Html5 FormData实现多文件上传
前一阵子,迭代一个线上的项目,其中有一个图片上传的功能,之前用的ajaxfileupload.js来实现上传的,不过由于ajaxfileupload.js,默认是单文件上传(虽然可以通过修改源码的方法 ...
- Kinect 常用识别手势
以下手势能被流畅的识别: ◎RaiseRightHand / RaiseLeftHand – 左手或右手举起过肩并保持至少一秒 ◎Psi –双手举起过肩并保持至少一秒 ◎Stop – 双手下垂. ◎W ...
- fedora下一些问题的解决方案汇总
解决fedora下一些使用问题 一 解决fedora下无法使用Fn+功能键来调整亮度的问题 在fedora下,背光的配置参数在/sys/class/backlight文件夹下,根据不同的显卡,有不同的 ...