月歸檔:四月 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來成全嗎?

發表在 某時雨集 | 留下評論

陰謀

你的頭髮留吧留吧,小心有一天終於我把你娶過門了,晚上看着你那順長,並且可以說屬於我了的秀髮陰陰地冷笑。

發表在 某時雨集 | 留下評論