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 ...
随机推荐
- js将格式化的时间转换成时间戳
var timestamp1 = Date.parse(new Date(startTime)), timestamp2 = Date.parse(new Date(endTime));;consol ...
- Windows PowerShell 默认颜色
屏幕背景:1,36,86 屏幕文字:238,237,240 弹出文字:0,128,128 弹出窗口背景:255,255,255
- Python库:序列化和反序列化模块pickle介绍
1 前言 在“通过简单示例来理解什么是机器学习”这篇文章里提到了pickle库的使用,本文来做进一步的阐述. 通过简单示例来理解什么是机器学习 pickle是python语言的一个标准模块,安装pyt ...
- web前端CSS2学习2017.6.17
CSS---表现层,修饰和表现html文档,为了解决结构层和表现层分离的问题. 通过CSS极大的提高了工作效率,方便工作人员维护和管理CSS:层叠样式表,目前用的最广泛的css版本为css2,最新版本 ...
- 代码规范--捡拾(SQL语句)
最近在看阿里的JAVA开发手册,话不多说进入正题. 1.[强制]不使用count(列名)或者是count(常量)代替count(*) 因为count(*)会统计NULL值,前面的两个不会 2.[强制] ...
- vijos1101题解
题目: 研究表明,这种传染病的传播具有两种很特殊的性质: 第一是它的传播途径是树型的,一个人X只可能被某个特定的人Y感染,只要Y不 得病,或者是XY之间的传播途径被切断,则X就不会得病. 第二是,这种 ...
- Codeforces Round #371 (Div. 2) 转换数字
C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Carbondata源码系列(一)文件生成过程
在滴滴的两年一直在加班,人也变懒了,就很少再写博客了,最近在进行Carbondata和hive集成方面的工作,于是乎需要对Carbondata进行深入的研究. 于是新开一个系列,记录自己学习Carbo ...
- Java用Cookie简单限制点赞次数
楼主最近在搞一个当下比较流行的点赞功能,这个功能也是让程序员又爱又恨啊 说起爱,点赞是个社会化的动作,全民都在为美好的事情,行为,动作,点赞. 说起恨,你很难在用户没有登录的情况下限制恶意点赞的机器人 ...
- 事务之使用JDBC进行事务的操作2
本篇将讲诉如何使用JDBC进行数据库有关事务的操作.在上一篇博客中已经介绍了事务的概念,和在MySQL命令行窗口进行开启事务,提交事务以及回滚事务的操作. 似乎事务和批处理都可以一次同时执行多条SQL ...