月归档:四月 2007

Learning Ruby 2 Classes, Objects, and Variables

with <Programming Ruby 2nd>
Chapter 3
Classes, Objects, and Variables
Class
class Song
  def initialize(name, artist, duration)
    @name     = name
    @artist   = artist
    @duration = duration
  end
end
initialize, which is always private
Instance variables are accessible to all the methods in an object, and each object has its own copy of its instance variables.

song.inspect → #<Song:0x1c7ca8 @name="Bicylops", @duration=260,
               @artist="Fleck">

In Ruby, classes are never closed.
class Song
  def to_s
    "Song: #@name–#@artist (#@duration)"
  end
end

Inheritance and Messages
class KaraokeSong < Song
      def initialize(name, artist, duration, lyrics)
        super(name, artist, duration)
        @lyrics = lyrics
      end
   
    def to_s
          super + " [#@lyrics]"
    end
end

If you don’t specify a parent when de?ning a class, Ruby supplies class Object as a default.

Ruby classes can include the functionality of any number of mixins (a mixin is like a partial class de?nition). This provides a controlled multiple-inheritance-like capability
with none of the drawbacks.

Objects and Attributes
class Song
  def name
    @name
  end
  def artist
    @artist
  end
  def duration
    @duration
  end
end

convenient shortcut:
class Song
  attr_reader :name, :artist, :duration
end
Symbol object — You can think of :artist as meaning the name of the variable artist, and plain artist as meaning the value of the variable.

Writable Attributes
class Song
  def duration=(new_duration)
    @duration = new_duration
  end
end
shortcut:
class Song
  attr_writer :duration
end

Virtual Attributes
class Song
  def duration_in_minutes
    @duration/60.0   # force floating point
  end
  def duration_in_minutes=(new_duration)
    @duration = (new_duration*60).to_i
  end
end
To the outside world, duration_in_minutes seems to be an attribute like any other. Internally, though, it has no corresponding instance variable.

Attributes, Instance Variables, and Methods
An attribute is just a method.
The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods.

Class Variables
A class variable is shared among all objects of a class.
class variables must be initialized before they are used.
class Song
  @@plays = 0
  def initialize(name, artist, duration)
    @name     = name
    @artist   = artist
    @duration = duration
    @plays    =0
  end
  def play
    @plays += 1   # same as @plays = @plays + 1
    @@plays += 1
    "This song: #@plays plays. Total #@@plays plays."
  end
end

Class Methods
class Example
  def instance_method      # instance method
  end
  def Example.class_method # class method
  end
end

class SongList
  MAX_TIME = 5*60           #  5 minutes
  def SongList.is_too_long(song)
    return song.duration > MAX_TIME
  end
end

Class Method De?nitions
class Demo
  def Demo.meth1
    # …
  end
  def self.meth2
    # …
  end
  class <<self
    def meth3
      # …
    end
  end
end

Singletons and Other Constructors
class MyLogger
  private_class_method :new #making MyLogger’s new method private
  @@logger = nil
  def MyLogger.create #provide a class method
    @@logger = new unless @@logger
    @@logger
  end
end

Access Control
• Public methods
• Protected methods
• Private methods
If a method is protected, it may be called by any instance of the de?ning class or its subclasses. If a method is private, it may be called only within the context of the calling object—it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller.

Specifying Access Control
private         # subsequent methods will be 'private'
    def method3
      #…
    end
#….

or

class MyClass
  def method1
  end
  # … and so on
  public    :method1, :method4
  protected :method2
  private   :method3
end

class Account
  attr_reader :balance # accessor method 'balance'
  protected :balance   # and make it protected

Variables
person1 = "Tim"
person2 = person1
person1[0] = 'J'
person1       "Jim"
          →
person2       "Jim"
          →
variable isn't an object
variables hold references to objects, not the objects themselves.

you could avoid aliasing by using the dup method of String, which creates a new String object with identical contents.
    person1 = "Tim"
    person2 = person1.dup
    person1[0] = "J"
    person1   → "Jim"
    person2   → "Tim"
You can also prevent anyone from changing a particular object by freezing it

发表在 Ruby on Rails | 留下评论

我要为它献出我的一辈子

今晚想来想去,最后我还是下了决心: 继想成为一位武功高强的侠客、值得尊敬的教育家、业界数一数二的3D建模师、技艺高超的二胡演奏家及优秀的越剧主胡、知名越剧编剧兼导演、顶尖的信息管理人才之后,我要把我的一生,全心全意地、毫无保留地奉献给相声事业。我要用我毕生的精力花费在相声的创作和表演上,我要用相声实现我的人生价值,我要用我的一生给别人带来快乐!
后面是什么词?对了,想起来了--
就从今晚七点开始!

发表在 某时雨集 | 留下评论

改良越剧

5.剧团公司化,自负盈亏.
1.越剧要打字幕,对白,唱词都要打字幕.字幕要用行书手写体,白色黑边,由下向上一行一行滚.
14.以唱为主,以表演和对白为辅。
2.越剧要在舞台上表演.表演多用传统符号形式的表达方式.
3.使用传统乐器伴奏. 传统,特色.
4.全部女性表演. 有特点才有存在的必要.
6.2年或一年出一新戏.电影化.商业化操作
7.分场,过场 用背景声 用地方名/时间,字幕打在幕上,
8,百姓喜欢的故事,不为申报官方的文化精品工程,与2对比
10.<五女拜寿>的故事节奏
12.删! 删! 删! 把3小时删成2小时半, 把2小时半删成2小时, 把2小时删到1小时半, 太多无关的情节和表演了. 删除+简化

尝试
13.<梅龙镇>的过场方式
8.第一场之前有序幕,如<梅龙镇>,如final fantasy
9.有主题曲,如<郎里郎*梅龙镇>
11.以类似 "佳话千年四海扬"的套话 结束
12.如<主奴联姻>,<送花篮>,<红楼梦>,多用低龄演员演出

发表在 越剧柔情 | 留下评论

看2007年央视春节晚会

今天才看春节晚会。本不想,但一看发现如此好看(第一个节目),如是就看到了结束。
看2007年央视春节晚会
好可爱的阿牛

看2007年央视春节晚会
可爱的阿牛引出一个春晚中最活泼的女孩

看2007年央视春节晚会
高手们

看2007年央视春节晚会
真正的高手不露相,今年的春晚显示的字幕很酷

看2007年央视春节晚会
没想到我心爱的越剧演员何赛飞上春晚了!

看2007年央视春节晚会
更多何赛飞

看2007年央视春节晚会
还是阿飞

看2007年央视春节晚会
又见何赛飞

看2007年央视春节晚会
再唱
看2007年央视春节晚会

看2007年央视春节晚会
不唱了,但要看完别人表演

发表在 其它 | 留下评论

窗外,1973

窗外,1973

琼瑶<窗外>应该是我读的第一部言情小说。当时读初中还是小学,不知道从哪翻出一本没有开头也没有封底的<窗外>,当时本着珍惜资源的原则和多识也个字的目的,断断续续把它读完了。直到今天看完<窗外>的电影版,我才想起 江雁容 和 康南 这两个熟悉的陌生人。

窗外,1973
台湾也流行女中

窗外,1973
好可爱的女学生。

窗外,1973这个老师太无耻了!

窗外,1973美丽的南台湾

活在幻想中的人要面对现实的种种问题,真的可以做到吗?电影在问。
可惜<窗外>不能证明什么,毕竟里面的人虽然努力过,但选择了放弃。
6

发表在 电影评论 | 一条评论

从夏志清的<中国现代小说史>读鲁迅

我是很喜欢鲁迅的,所以总是想更多地了解他。今天翻了一下<中国现代小说史>,里面有一章是专讲鲁迅的,其中有很多精妙的见解,我把它们抄了下来:

在三十年代和四十年代期间,这个鲁迅的神话对于共产党特别有帮助,因为他的作品可以用来加强国民党贪污和腐败的印象。今天这个神话的效用已经有点过时,不过把鲁迅仍视为国家英雄,对于中共政权还是有得的,虽然中共却极力阻止任何人模仿他的讽刺文体。

鲁迅的<肥皂>与<离婚>是描写士大夫与农人的两篇优秀小说,对复杂的风俗习惯与伦理道德的探讨,深入得令人看了觉得恐怖。

鲁迅可以不顾逻辑和事实,而无情地打击他的敌人,证明自己永远是对的。

作为讽刺民国成立二十年来的坏风恶习来看,鲁迅的杂文非常有娱乐性,但是因为他基本的观点不多--即使是发挥得淋漓尽致--所以他十本杂文给人的总印象是搬弄是非、罗罗嗦嗦。(或是因为他自己也没有明确,坚信的观点吧。但正如上面说的,鲁迅对个人的攻击力却是超强的)

大体上说来,鲁迅为其时代所摆布,而不能算是他那时代的导师和讽刺家。

读完这一章,感觉最后一句说得特别好。鲁迅好像是想做,并也被称为青年的“导师”的。但我感觉他最本质的思想基调却是“彷徨”。

发表在 其它 | 2 条评论

祭2007年情人节

在这个特别的日子里,见到她的第一刻,我就暗暗下定决心--在明年的今天,我一定要手捧一大束红玫瑰,
像她一样,在街上叫卖!

发表在 某时雨集 | 留下评论

一生

不小心我从21楼的窗户滑了出去。我就这样完了?我不能放弃!我迅猛地用双手抓着20楼的阳台。。。抓空了。。。又滑过了18楼的窗檐。。但我没有放弃。。终于我摸到了17楼的栏杆。。。在16楼时我掏到了一支挂在阳台的一个拖把。。。我没有放弃。。。于是我在14楼扯下了一条裤子。。。如果11楼那条网线没有断的话我就要获救了。。。我还没有放弃。。。后面是7楼的水管,可惜太滑了。。。。没关系,我并没有放弃,5楼那还有。。。太晚了,我还没看清5楼阳台上有什么。。“啪”的一声,我的脑子碎开了。
“为什么是这样?我是热爱生命的。我从来都没有放弃过我的生命。为什么是这样?“我大叫了起来。
“哼呵呵呵。。。不,你放弃了!!!”一个深沉回声带着刺耳的嘲笑向传向我的耳朵,“从21楼飞出去的生命可以是多么美丽,冷静地展开双臂,从容地放下一却欲望和爱恨苦乐,府看人间,让自由的心灵带着身体悠闲地飞翔。。。可是你?! 你放弃了!正如你放弃了之前20年的生命一样,你在‘不放弃‘中放弃了你的一生!“

发表在 某时雨集 | 留下评论

mp3播放器

眼睛好痛啊。 一个下午就为了在网上挑一个合适的mp3????? 难道我的幸福就等一个美完的mp3来成全吗?

发表在 某时雨集 | 留下评论

阴谋

你的头发留吧留吧,小心有一天终于我把你娶过门了,晚上看着你那顺长,并且可以说属于我了的秀发阴阴地冷笑。

发表在 某时雨集 | 留下评论