20210410のRubyに関する記事は13件です。

sendメソッドを調べてみた

はじめに railsで複数のカラムに、ある処理をして出力された値を入れたい場面で、sendメソッドが使用されていたので調べてました。 sendメソッドとは レシーバが持っているメソッドをして、呼び出すメソッド 構文 send(name, *args) name: メソッド名(文字列かシンボルで指定) args: メソッドに渡す引数 使用場面 条件別にメソッドを呼び出したい、カラムに値を入れたい時に使用する サンプルコード class Exam def japanese(score) puts "国語は#{score}点です" end def math(score) puts "数学は#{score}点です" end def english(score) puts "英語は#{score}点です" end end exam = Exam.new exam.send("japanese", 74) # => 国語は74点です exam.send(:math, 69) # => 数学は69点です exam.send(:english, 58) # => 英語は58点です *カラムに値を入れる場合、 send("カラム名=", 入れたい値) =を忘れないよう注意する必要がある。 # テーブル作成してある程で。 exam = Exam.new exam.send("japanese=", 74) exam.japanese => 74 おわりに 'はじめに'で話した使用場面では、 name: 複数カラムを配列に格納 args: あるメソッド通って出力された値 に当てはめ、each文を使用して、3行で該当するカラム全てに値を入れ直すことが出来ていたので便利に感じた。 参考記事 【Ruby on Rails】sendメソッドのいろんな書き方 Rubyのsendメソッドを使用するメリットと注意点 Object#send (Ruby 3.0.0 リファレンスマニュアル)
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

PHPの標準関数をRubyで書く(文字列操作編)

はじめに PHPの標準関数をRubyで書いてみました。 簡単に書けそうなものだけパズル感覚で書いてます。 文字列操作編です。 引数と返り値の型はテーブルで記載しました。 str_contains($haystack, $needle) PHP: str_contains - Manual haystack String needle String return TrueClass | FalseClass Ruby haystack.include?(needle) str_ends_with($haystack, $needle) PHP: str_ends_with - Manual haystack String needle String return TrueClass | FalseClass Ruby haystack.end_with?(needle) str_getcsv ($string, $separator, $enclosure, $escape) PHP: str_getcsv - Manual string String separator String enclosure String escape 標準CSVライブラリでは指定不可(2回続けて入力してエスケープ) return Array<String> Ruby require "csv" CSV.parse_line(string, col_sep: separator, quote_char: enclosure) str_ireplace($search, $replace, $subject, &$count) PHP: str_ireplace - Manual search String replace String subject String count Integer return String Ruby count = 0 subject.gsub(Regexp.new(Regexp.escape(search), Regexp::IGNORECASE)) {count += 1; replace} str_pad($string, $length, $pad_string, $pad_type) PHP: str_pad - Manual string String length Integer pad_string String pad_type STR_PAD_RIGHT return String Ruby string.ljust(length, pad_string) string String length Integer pad_string String pad_type STR_PAD_LEFT return String Ruby string.rjust(length, pad_string) string String length Integer pad_string String pad_type STR_PAD_BOTH return String Ruby string.center(length, pad_string) str_repeat($string, $times) PHP: str_repeat - Manual string String times Integer return String Ruby string * times str_replace($search, $replace, $subject, &$count) PHP: str_replace - Manual search String replace String subject String count Integer return String Ruby count = 0 subject.gsub(Regexp.new(Regexp.escape(search))) {count += 1; replace} str_rot13($string) PHP: str_rot13 - Manual string String return String Ruby string.tr("a-zA-Z", "n-za-mN-ZA-M") str_shuffle($string) PHP: str_shuffle - Manual string String return String Ruby string.chars.shuffle.join str_split($string, $length) PHP: str_split - Manual string String length Integer ※1以上 return String Ruby string.each_char.each_slice(length).map(&:join) str_starts_with($haystack, $needle) PHP: str_starts_with - Manual haystack String needle String return TrueClass | FalseClass Ruby haystack.start_with?(needle) str_word_count($string, $format, $characters) PHP: str_word_count - Manual string String format 0 characters String return Integer Ruby string.match(/^[-']*(.*?)-*$/)[1].scan(/[-'a-zA-Z]+/).count string String format 1 characters String return Array<String> Ruby string.match(/^[-']*(.*?)-*$/)[1].scan(/[-'a-zA-Z]+/) strcasecmp($str1, $str2) PHP: strcasecmp - Manual str1 String str2 String return Integer Ruby str1.casecmp(str2) ※返却値は-1, 0, 1のいずれかなので注意 strcmp($str1, $str2) PHP: strcmp - Manual str1 String str2 String return Integer Ruby str1 <=> str2 ※返却値は-1, 0, 1のいずれかなので注意 strcspn($string, $characters, $offset, $length) PHP: strcspn - Manual string String characters String offset Integer length Integer ※0以上 return Integer Ruby string = string[offset, length] string.chars.each_with_index.find { |c, _| characters.include?(c) }&.at(1) || string.length stripos($haystack, $needle, $offset) PHP: stripos - Manual haystack String needle String offset Integer return String | FalseClass Ruby haystack.downcase.index(needle.downcase, offset) stristr($haystack, $needle, $before_needle) PHP: stristr - Manual haystack String needle String before_needle FalseClass return String | FalseClass Ruby haystack.chars.each_with_index.find { |c, _| c.casecmp?(needle) }.then { |_, i| i ? haystack[i..] : false } haystack String needle String before_needle TrueClass return String | FalseClass Ruby haystack.chars.each_with_index.find { |c, _| c.casecmp?(needle) }.then { |_, i| i ? haystack[0, i] : false } strlen($string) PHP: strlen - Manual string String return Integer Ruby string.length strncasecmp($str1, $str2, $length) PHP: strncasecmp - Manual str1 String str2 String length Integer return Integer Ruby str1[0, length].casecmp(str2[0, length]) ※返却値は-1, 0, 1のいずれかなので注意 strncmp($str1, $str2, $length) PHP: strncmp - Manual str1 String str2 String length Integer return Integer Ruby str1[0, length] <=> str2[0, length] ※返却値は-1, 0, 1のいずれかなので注意 strpbrk($string, $characters) PHP: strpbrk - Manual string String characters String return String | FalseClass Ruby string.chars.each_with_index.find { |c, _| characters.include?(c) }.then { |_, i| i ? string[i..] : false } strpos($haystack, $needle, $offset) PHP: strpos - Manual haystack String needle String offset Integer return String | FalseClass Ruby haystack.index(needle, offset) strrchr($haystack, $needle) PHP: strrchr - Manual haystack String needle String return String | FalseClass Ruby haystack.chars.reverse.each_with_index.find { |c, _| c == needle.chr }.then { |_, i| i ? haystack[-i-1..] : false } strrev($string) PHP: strrev - Manual string String return String Ruby string.reverse strripos($haystack, $needle, $offset) PHP: strripos - Manual haystack String needle String offset Integer ※0以上 return String | FalseClass Ruby haystack[offset..].downcase.rindex(needle.downcase)&.+(offset) || false haystack String needle String offset Integer ※0未満 return String | FalseClass Ruby haystack.downcase.rindex(needle.downcase, offset) || false strrpos($haystack, $needle, $offset) PHP: strrpos - Manual haystack String needle String offset Integer ※0以上 return String | FalseClass Ruby haystack[offset..].rindex(needle)&.+(offset) || false haystack String needle String offset Integer ※0未満 return String | FalseClass Ruby haystack.rindex(needle, offset) || false strspn($string, $characters, $offset, $length) PHP: strspn - Manual string String characters String offset Integer length Integer ※0以上 return Integer Ruby string = string[offset, length] string.chars.each_with_index.find { |c, _| !characters.include?(c) }&.at(1) || string.length strstr($haystack, $needle, $before_needle) PHP: strstr - Manual haystack String needle String before_needle FalseClass return String | FalseClass Ruby haystack.chars.each_with_index.find { |c, _| c == needle }.then { |_, i| i ? haystack[i..] : false } haystack String needle String before_needle TrueClass return String | FalseClass Ruby haystack.chars.each_with_index.find { |c, _| c == needle }.then { |_, i| i ? haystack[0, i] : false } strtolower($string) PHP: strtolower - Manual string String return String Ruby string.downcase strtoupper($string) PHP: strtoupper - Manual string String return String Ruby string.upcase strtr($string, $from, $to) PHP: strtr - Manual string String from String to String return String Ruby limit = [from, to].map(&:length).min string.tr(from[0, limit], to[0, limit]) strtr($string, $replace_pairs) PHP: strtr - Manual string String replace_pairs Hash<String, String> return String Ruby string.gsub(Regexp.new(replace_pairs.keys.map { |s| Regexp.escape(s) }.join('|'))) { |m| replace_pairs[m] } 参考文献 Class array (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Array.html Class CSV (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/CSV.html Class Regexp (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Regexp.html Class string (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/String.html Module enumerable (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Enumerable.html Str_contains. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-contains.php Str_ends_with. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-ends-with.php Str_getcsv. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-getcsv.php Str_ireplace. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-ireplace.php Str_pad. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-pad.php Str_repeat. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-repeat.php Str_replace. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-replace.php Str_rot13. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-rot13.php Str_shuffle. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-shuffle.php Str_split. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-split.php Str_starts_with. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-starts-with.php Str_word_count. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-word-count.php Strcasecmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strcasecmp.php Strcmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strcmp.php Strcspn. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strcspn.php Stripos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.stripos.php Stristr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.stristr.php Strlen. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strlen.php Strncasecmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strncasecmp.php Strncmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strncmp.php Strpbrk. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strpbrk.php Strpos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strpos.php Strrchr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strrchr.php Strrev. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strrev.php Strripos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strripos.php Strrpos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strrpos.php Strspn. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strspn.php Strstr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strstr.php Strtolower. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strtolower.php Strtoupper. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strtoupper.php Strtr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strtr.php
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

heroku logs --tail --app<アプリ名>を入力した際、ActionView::Template::Error (Mysql2::Error: Table --- doesn't exist):が出てきた時の対処法。

エラー文を入力して検索。様々な原因があるようですが、私の場合はmigration fileに  --NO FILE--が存在することが原因でした。 rails db:rollbackで該当のファイルを差し戻し、手動で消去。rails db:migrateコマンドを実行。 statusコマンドを実行し存在しないことを確認。本番環境に適応させるために、heroku run rails db:migrateを実行。再びheroku logs --tail --app <アプリ名>を実行。エラー分が消えていることを確認。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

ruby even?メソッド

初めに 初学者です。学習した内容が定着させるためにアウトプットしていきます。 even?メソッド even?メソッドとはRubyがもともと用意している、対象の数値が偶数かどうか判別するためのメソッドになります。 対象の数値が偶数の場合は真を返し、そうでない場合は偽を返します。 2.even? #=> true 3.even? #=> false 配列の中身の偶数の数をカウントしてみましょう。 array = [1,2,3] count = 0 array.each do |num| if num.even? count += 1 end puts count end 変数countを定義してカウントした数を保持する変数を作ります。 そしてif文を使用して偶数の時だけカウントするように記述します。 最後にcountを出力すれば完成です。 今回は配列arrayには偶数が1つしかないので1と答えがですはずです。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Ruby 変数について

Ruby変数に関しての記事を書きたいと思う。 変数 ■変数 変数とは、値を入れる箱のようなもの。この箱には名前をつけることができ、どんな値が入っているのかを簡単に識別できるようになる。この名前を変数名と言う。 変数を利用することで、値を再利用したい場合に、変数の名前を使うだけで呼び出すことができる。 ■変数の宣言と定義 変数を作ることを変数の宣言と言う。このとき、作成する変数には名前をつける。 そして宣言した変数にどのような値を入れるのか記述することを変数の定義言う。 fruits #宣言(変数名をつけて変数を作る) fruits = "apple" #定義(変数にどのような値を入れるか記述する) #基本的には、宣言と定義は同時に行う。 ■代入 変数のあとの=(イコール)は数学の方程式のように等しいという意味ではなく、 「変数の箱の中に値を入れる」という意味。これを代入と言う。 この=は代入演算子と呼ばれる。 fruitsが左辺、appleが右辺「=」を使って、右辺を左辺に代入している。 ■再代入 1度値を代入したあとの変数に、別の値を再び代入することもできる。 プログラム中に何度でも変更可能。 変数の値が後から代入したものに書き換わっている。 Rubyでは、変数にはどんな値でも代入できる。 変数に対してメソッドを使うこともできる。 上記のように、1を入れた変数の中に1を足して2を入れた変数にすることも可能。 この方法を自己代入変算子と言う。 自己代入演算子 例 処理 += number += 1 numberに1足した値をnumber自身に代入 -= number -= 1 numberから1引いた値をnumber自身に代入 *= number *= 2 numberに2かけた値をnumber自身に代入 /= number /= 2 numberを2で割った値をnumber自身に代入 変数の命名規則 命名規則 説明 変数の中身が何かわかる どんな名前もつけられるが、zzzのような意味のない名前は避ける。 小文字で始める 大文字からも開始できるが、とくに理由がない場合は避ける。 _(アンダーバー)で始めない _からも開始できるが、とくに理由がない場合は避ける。2文字目以降には使える。 数字で始めない 1文字目に使うとエラーが生じる。2文字目以降には使える。 日本語を使わない 文字列以外では日本語は使えないため、変数名も日本語は使えない。 スペースを含めない 名前にスペースが入るとエラーが生じる。 予約語を使用しない Rubyには文法などで使うことがあらかじめ決まっている単語があり、それを予約語と呼ぶ。使うとエラーが生じる。 ソースコードの見やすさ(可読性)や分かりやすさは、プログラミングにおいて非常に重要なため、わかりやすい 名前をつける。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

emptyの表記方法

どこで調べたかは忘れたけど empty?にはこういう表記方法もあるようだ <% if @items.empty?.! %> <% @items.each do |item| %> <li class='list'> <%= link_to item_path(item.id) do %> <div class='item-img-content'> <%= image_tag item.image, class: "item-img" %> emptyは文字列が空の時、真を返すが emptyの後ろに.!をつけると反対の意味になるようだ
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

macOS Big Sur で eventmachine がインストールできないときの対処法

結果だけ知りたい Shell sudo xcode-select --switch /Applications/Xcode.app sudo xcodebuild -license accept gem install eventmachine 環境 macOS Big Sur 11.2.3 Xcode 12.4 Ruby 2.7.1 RubyGems 3.1.2 Bundler 2.1.4 eventmachine 1.2.7 システムのバージョン詳細: Brewfile.lock.json --with-cppflags ではダメだった macOS に eventmachine をインストールしようとしたら失敗した。 ネットで記事を調べると、以下の解決法しか出てこないが、これだとインストールできなかった。 Shell gem install eventmachine -- --with-cppflags=-I/usr/local/opt/openssl/include ログを見る 真面目にログを見てみる。 /Users/noraworld/.anyenv/envs/rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/extensions/x86_64-darwin-19/2.7.0/eventmachine-1.2.7/mkmf.log xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun checked program was: /* begin */ 1: #include "ruby.h" 2: 3: int main(int argc, char **argv) 4: { 5: return !!argv[argc]; 6: } /* end */ "pkg-config --exists openssl" package configuration for openssl is not found package configuration for openssl is not found package configuration for openssl is not found が気になる。 でも、OpenSSL はちゃんと最新版がインストールされているし、--with-ssl-include とか --with-cppflags とか --with-openssl-config とか、OpenSSL に関連しそうなオプションをいろいろ付けてみたけどダメだった。 package configuration for openssl is not found って出てくるけど、どうやら OpenSSL の問題だけではなさそう。 xcrun: error: invalid active developer path 次に気になるのは xcrun: error: invalid active developer path だ。 CommandLineTools のパスがなんかおかしいと言われている。 ここで、ふと、自分が以前に書いた記事を思い出した。 すると、過去にも全く同じ警告が出ていることがわかった。 brew doctor そこで、brew doctor を試してみた。 Shell brew doctor Warning: Your Xcode is configured with an invalid path. You should change it to the correct path: sudo xcode-select --switch /Applications/Xcode.app どうやらこれを実行すれば良さそう。 Shell sudo xcode-select --switch /Applications/Xcode.app その後、もう一度 brew doctor を実行すると、以下のエラーが出た。 Shell brew doctor Error: You have not agreed to the Xcode license. Please resolve this by running: sudo xcodebuild -license accept ライセンスに同意していないので同意してくれということらしいので、同意する。 Shell sudo xcodebuild -license accept そして、さらにもう一度 brew doctor を実行すると、警告が消えた。 インストール完了 ここで、最初に戻り、eventmachine をインストールしようとしたら、インストールできた。 gem install eventmachine 結局、オプション (--with-ssl-include とか --with-cppflags とか --with-openssl-config とか) は要らなかった。 結論 困ったときは、brew doctor。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Webpacker::Manifest::MissingEntryError in Home#topの対処

railsで新規appwを作製し、サーバーを立ち上げアクセスしたところエラーが出てしまったので対処しました やったこと 1.node.jsの削除 2.node.js安定版のインストール 3. $rails webpacker:install 4. $rails webpacker:compile 以上でできたのでお試しあれ。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

プログラミングしてて調べた英単語集(随時更新)

Webアプリケーションを開発してて調べた英単語たちをメモ的にあげていきます。 単語がWEB用語であるかどうかは関係なしにあげていきます。 単語力の低さは目をつぶってください。。 A aim 意図する。狙う。 出てきたとこ RubocopのDocument B C clause 句。条項。項。 出てきたとこ Ruby(Rubocopに"Use a guard clause"って怒られた) guard clause: 「ガード節」「ガード条件」「ガード構文」 参考: [Ruby/Rails] 例外で深くなったネストをGuard Clauseですっきりさせる concatenation 連結、連なり 出てきたとこ Rubocop configure 構成、設定 出てきたとこ あちこち。 .conf とか config はこの子の略。 conservative 保守的 出てきたとこ RubocopのDocument context 環境。文脈 出てきたとこ Rspec D describe 説明する 出てきたとこ Rspec E ease 簡易。楽。 出てきたとこ RubocopのDocument enumerate 列挙する 出てきたとこ git pushの結果 extention 拡張 出てきたとこ RubocopのDocument extract 引き抜く 出てきたとこ RubocopのDocument F G H I implement 実装する 出てきたとこ Rspec interpolation 書き入れ、改ざん、補間 出てきたとこ Rubocop J K L M miscellaneous その他。雑多な。 出てきたとこ Railsチュートリアル N O P parser 構文解析を行うためのプログラム 出てきたとこ Rubocop prevent 防ぐ 出てきたとこ RubocopのDocument Q R reserve 予約する。留保する。取っておく。 出てきたとこ RubocopのDocument S stable 安定している 出てきたとこ RubocopのDocument T trait 特性 出てきたとこ Rspec U V W X Y Z
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

テストコード(単体テスト)エラー

テストコードのコマンドを入力すると、今まで見た事のないエラー文が。 ActiveRecord::StatementInvalid: Mysql2::Error::ConnectionError: Lost connection to MySQL server during query 上記をコピペしてググることに。いっぱい出てきました。 何やらテストコードを精製してるspec.rbに以下の一文を加えたら良いらしい。 before do @user = FactoryBot.create(:user) @item = FactoryBot.create(:item) @order_address = FactoryBot.build(:order_address,user_id: @user.id, item_id: @item.id) sleep 0.1      ⬅️こちらです! end 上記は生成するのに0.1秒待機させて処理する仕組みのようです。 結果、全て通るようになりました!
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【個人開発】制限のない自由なSNS作ってみた!

初めに この度Ammotというwebアプリを開発しました。 コンセプトは「制限の少ない自由な投稿を」です。 「ツイッターは文字数制限がきつい、ただfacebookは実名制だしデザインがごちゃごちゃでいや」 という声を聴いたので作ってみました。 先に行っておくと Ammotは文字数制限が6000字まで、画像・動画・PDF・音声は同時に10個まで投稿可能です。 概要 ↑ログイン前のトップページにはフィットスクロール(名前間違いかも)を使用して新しいおしゃれなサイト感を出してます。 ↑これがログイン後のトップです。フォローしたユーザーの投稿がHOMEに流れて、 サイドバーにはおすすめの投稿と現在人気のタグを表示しています。 ↑ポップアップの投稿画面です。 ツイッターのいやなとこ 文字数制限 編集ができない(個人的にめちゃ嫌) facebookのいやなとこ 制限はないがデザインがぐちゃぐちゃ 実名制はなんかいやだ この二つSNSの問題を解決するのがAmmotです。 工夫した点 無料プランにこだわった こういうものはバズらない限り、すぐには成功しません。使用者が全くいなくても1年は公開しようと思います。 でも使用者がいないのに月数百円も取られるの馬鹿馬鹿しいじゃないですか。だからドメイン以外はすべて無料です。 ただherokuの無料プランだとドメインを設定できないのでCloudflareを使い設定しました。 参考記事: デザインとUIにこだわった(つもり) フィットスクロール?だったり投稿画面のポップアップだったり 今まで使ってこなかった手法を使ってみました。 自己満になってるかもしれない ちなみにデザインを参考にしたサイトはtumblrです。 比べればまぁまぁ似てると思います。 機能性 最初にも書きましたが画像・動画・PDF・音声の投稿とプレビュー機能を実装しました。 これにはいろんな記事がありましたが まともに動きかつ一番簡単でコードもシンプルなこの記事を参考にするのをお勧めします。 使用した技術 rails6 ruby2.7 postgresql heroku free aws s3 gem 'ridgepole' gem 'slim-rails' gem 'html2slim' gem 'pry-rails' gem 'devise' gem 'kaminari' gem 'activeadmin' gem 'rack-attack' gem 'rails-i18n' gem 'devise-i18n' gem 'devise-i18n-views' gem 'carrierwave' gem 'rmagick' gem 'rinku' gem 'fog-aws' gem 'dotenv-rails' #一部抜いてます まとめ 今回の開発は今までやらなかったことにめちゃ挑戦できたので楽しかったです 苦手なjquery・javascriptもプレビュー機能のおかげでちょっとわかった気がしてます。 文字数制限や実名制で不便を感じたことのある人はぜひAmmot使ってください!お願いします。 ↓URL https://ammot.net/ ↓僕のAmmotのアカウント https://ammot.net/user/yamada ↓僕のツイッターのアカウント https://twitter.com/yamada1531
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

制限の少ない自由なSNS

初めに この度Ammotというwebアプリを開発しました。 コンセプトは「制限の少ない自由な投稿を」です。 「ツイッターは文字数制限がきつい、ただfacebookは実名制だしデザインがごちゃごちゃでいや」 という声を聴いたので作ってみました。 先に行っておくと Ammotは文字数制限が6000字まで、画像・動画・PDF・音声は同時に10個まで投稿可能です。 概要 ↑ログイン前のトップページにはフィットスクロール(名前間違いかも)を使用して新しいおしゃれなサイト感を出してます。 ↑これがログイン後のトップです。フォローしたユーザーの投稿がHOMEに流れて、 サイドバーにはおすすめの投稿と現在人気のタグを表示しています。 ↑ポップアップの投稿画面です。 ツイッターのいやなとこ 文字数制限 編集ができない(個人的にめちゃ嫌) facebookのいやなとこ 制限はないがデザインがぐちゃぐちゃ 実名制はなんかいやだ この二つSNSの問題を解決するのがAmmotです。 工夫した点 無料プランにこだわった こういうものはバズらない限り、すぐには成功しません。使用者が全くいなくても1年は公開しようと思います。 でも使用者がいないのに月数百円も取られるの馬鹿馬鹿しいじゃないですか。だからドメイン以外はすべて無料です。 ただherokuの無料プランだとドメインを設定できないのでCloudflareを使い設定しました。 参考記事: デザインとUIにこだわった(つもり) フィットスクロール?だったり投稿画面のポップアップだったり 今まで使ってこなかった手法を使ってみました。 自己満になってるかもしれない ちなみにデザインを参考にしたサイトはtumblrです。 比べればまぁまぁ似てると思います。 機能性 最初にも書きましたが画像・動画・PDF・音声の投稿とプレビュー機能を実装しました。 これにはいろんな記事がありましたが まともに動きかつ一番簡単でコードもシンプルなこの記事を参考にするのをお勧めします。 使用した技術 rails6 ruby2.7 postgresql heroku free aws s3 gem 'ridgepole' gem 'slim-rails' gem 'html2slim' gem 'pry-rails' gem 'devise' gem 'kaminari' gem 'activeadmin' gem 'rack-attack' gem 'rails-i18n' gem 'devise-i18n' gem 'devise-i18n-views' gem 'carrierwave' gem 'rmagick' gem 'rinku' gem 'fog-aws' gem 'dotenv-rails' #一部抜いてます まとめ 今回の開発は今までやらなかったことにめちゃ挑戦できたので楽しかったです 苦手なjquery・javascriptもプレビュー機能のおかげでちょっとわかった気がしてます。 文字数制限や実名制で不便を感じたことのある人はぜひAmmot使ってください!お願いします。 ↓URL https://ammot.net/ ↓僕のAmmotのアカウント https://ammot.net/user/yamada ↓僕のツイッターのアカウント https://twitter.com/yamada1531
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Ruby】生年月日から年齢を計算するメソッド

生年月日から年齢を計算するメソッドを作成 まず、Dateクラスは組み込みクラスとはなっていない為、Dateクラスを使うにはまずプログラムファイルの中で次の一文を記述します。 require "date" 生年月日から年齢を計算する方法 (今日 - 生年月日) ÷ 10000 なので、 例)2000年4月9日生まれの人 (20210410 - 20000409) ÷ 10000 = 21.0001 → 21歳 例)2000年4月11日生まれの人 (20210410 - 20000411) ÷ 10000 = 20.9999 → 20歳 といった感じになる。 いざ実装 def get_age(birthday) today = Date.today.strftime("%Y%m%d").to_i birthday = birthday.strftime("%Y%m%d").to_i return (today - birthday) / 10000 end get_age(2010-12-25) Date.today で、 2020-04-10 といった感じで出力された値を、 strftime("%Y%m%d") で、20200410の形に変える。(.to_i で数値に変える) 引数 birthday でも同じ処理をする。 (birthday が 2010-12-25 の場合、 20101225) strftimeについては以下の内容を参考 https://www.sejuku.net/blog/44796 そして、 return (today - birthday) / 10000 で計算結果を返す。 (20200410 - 20101225) ÷ 10000 = 9.9185 なので、9 が出力され、9歳ということがわかる。 以上、ありがとうございました。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む