Created
January 26, 2009 12:27
-
-
Save cypok/52800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [3, 4, 5] | |
b = %w{a b c} # b = ['a', 'b', 'c'] - быстрая инициализация строковых массивов | |
a.each do |digit| | |
puts digit.to_s # конвертируем в строку (хотя тут это необязательно) | |
end | |
# поиграй с этим в irb - интерактивная консоль для ruby | |
a + b | |
a - b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# обратить внимание что все числа - это true | |
# false - дают только два объекта, это nil и собственно false | |
!!3 # true | |
!!"325" # true | |
!!0 # true | |
!!nil # false | |
!!false # false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cool | |
def initialize(var) | |
@var = var || 0 # переменная объекта (если был передан nil то использовать 0) | |
temp = 543 # локальная переменная | |
@@global = true # переменная класса | |
end | |
def blah | |
@var | |
end | |
end | |
c = Cool.new 4 | |
c.blah # => 4 | |
c = Cool.new nil | |
c.blah # => 0 | |
c = Cool.new 5 | |
# c.var = 6 # error, нету метода "var=" | |
# добавим его! | |
class << c | |
def var=(x) | |
@var = x | |
end | |
end | |
c.var = 6 | |
c.blah # => 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo(var) | |
puts var | |
end | |
foo "это по пацански" | |
foo("а это не очень") | |
foo( "иногда приходится делать так, но это лучше предыдущего" ) | |
foo ("а на это рельсы даже warning дадут") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def max(a, b) | |
if a > b | |
a | |
else | |
b | |
end | |
end | |
def valid(var) | |
return false if var.bad? | |
........ | |
end | |
max 3, 5 # демонстрация того факта, что любая функция возвращает последнее вычисленное значение | |
# то есть для возвращения результата необязательно писать return | |
# return используют только для досрочного прекращения работы (в функции valid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment